sata_promise.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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_dma_pkt(struct ata_taskfile *tf,
  380. dma_addr_t sg_table,
  381. unsigned int cdb_len, u8 *cdb,
  382. u8 *buf)
  383. {
  384. u32 *buf32 = (u32 *) buf;
  385. /* set control bits (byte 0), zero delay seq id (byte 3),
  386. * and seq id (byte 2)
  387. */
  388. if (!(tf->flags & ATA_TFLAG_WRITE))
  389. buf32[0] = cpu_to_le32(PDC_PKT_READ);
  390. else
  391. buf32[0] = 0;
  392. buf32[1] = cpu_to_le32(sg_table); /* S/G table addr */
  393. buf32[2] = 0; /* no next-packet */
  394. /* we can represent cdb lengths 2/4/6/8/10/12/14/16 */
  395. BUG_ON(cdb_len & ~0x1E);
  396. buf[12] = (((cdb_len >> 1) & 7) << 5) | ATA_REG_DATA | PDC_LAST_REG;
  397. memcpy(buf+13, cdb, cdb_len);
  398. }
  399. static void pdc_qc_prep(struct ata_queued_cmd *qc)
  400. {
  401. struct pdc_port_priv *pp = qc->ap->private_data;
  402. unsigned int i;
  403. VPRINTK("ENTER\n");
  404. switch (qc->tf.protocol) {
  405. case ATA_PROT_DMA:
  406. ata_qc_prep(qc);
  407. /* fall through */
  408. case ATA_PROT_NODATA:
  409. i = pdc_pkt_header(&qc->tf, qc->ap->prd_dma,
  410. qc->dev->devno, pp->pkt);
  411. if (qc->tf.flags & ATA_TFLAG_LBA48)
  412. i = pdc_prep_lba48(&qc->tf, pp->pkt, i);
  413. else
  414. i = pdc_prep_lba28(&qc->tf, pp->pkt, i);
  415. pdc_pkt_footer(&qc->tf, pp->pkt, i);
  416. break;
  417. case ATA_PROT_ATAPI:
  418. case ATA_PROT_ATAPI_NODATA:
  419. ata_qc_prep(qc);
  420. break;
  421. case ATA_PROT_ATAPI_DMA:
  422. ata_qc_prep(qc);
  423. pdc_atapi_dma_pkt(&qc->tf, qc->ap->prd_dma, qc->dev->cdb_len, qc->cdb, pp->pkt);
  424. break;
  425. default:
  426. break;
  427. }
  428. }
  429. static void pdc_freeze(struct ata_port *ap)
  430. {
  431. void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  432. u32 tmp;
  433. tmp = readl(mmio + PDC_CTLSTAT);
  434. tmp |= PDC_IRQ_DISABLE;
  435. tmp &= ~PDC_DMA_ENABLE;
  436. writel(tmp, mmio + PDC_CTLSTAT);
  437. readl(mmio + PDC_CTLSTAT); /* flush */
  438. }
  439. static void pdc_thaw(struct ata_port *ap)
  440. {
  441. void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  442. u32 tmp;
  443. /* clear IRQ */
  444. readl(mmio + PDC_INT_SEQMASK);
  445. /* turn IRQ back on */
  446. tmp = readl(mmio + PDC_CTLSTAT);
  447. tmp &= ~PDC_IRQ_DISABLE;
  448. writel(tmp, mmio + PDC_CTLSTAT);
  449. readl(mmio + PDC_CTLSTAT); /* flush */
  450. }
  451. static void pdc_error_handler(struct ata_port *ap)
  452. {
  453. ata_reset_fn_t hardreset;
  454. if (!(ap->pflags & ATA_PFLAG_FROZEN))
  455. pdc_reset_port(ap);
  456. hardreset = NULL;
  457. if (sata_scr_valid(ap))
  458. hardreset = sata_std_hardreset;
  459. /* perform recovery */
  460. ata_do_eh(ap, ata_std_prereset, ata_std_softreset, hardreset,
  461. ata_std_postreset);
  462. }
  463. static void pdc_post_internal_cmd(struct ata_queued_cmd *qc)
  464. {
  465. struct ata_port *ap = qc->ap;
  466. if (qc->flags & ATA_QCFLAG_FAILED)
  467. qc->err_mask |= AC_ERR_OTHER;
  468. /* make DMA engine forget about the failed command */
  469. if (qc->err_mask)
  470. pdc_reset_port(ap);
  471. }
  472. static void pdc_eng_timeout(struct ata_port *ap)
  473. {
  474. struct ata_host *host = ap->host;
  475. u8 drv_stat;
  476. struct ata_queued_cmd *qc;
  477. unsigned long flags;
  478. DPRINTK("ENTER\n");
  479. spin_lock_irqsave(&host->lock, flags);
  480. qc = ata_qc_from_tag(ap, ap->active_tag);
  481. switch (qc->tf.protocol) {
  482. case ATA_PROT_DMA:
  483. case ATA_PROT_NODATA:
  484. ata_port_printk(ap, KERN_ERR, "command timeout\n");
  485. drv_stat = ata_wait_idle(ap);
  486. qc->err_mask |= __ac_err_mask(drv_stat);
  487. break;
  488. default:
  489. drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
  490. ata_port_printk(ap, KERN_ERR,
  491. "unknown timeout, cmd 0x%x stat 0x%x\n",
  492. qc->tf.command, drv_stat);
  493. qc->err_mask |= ac_err_mask(drv_stat);
  494. break;
  495. }
  496. spin_unlock_irqrestore(&host->lock, flags);
  497. ata_eh_qc_complete(qc);
  498. DPRINTK("EXIT\n");
  499. }
  500. static inline unsigned int pdc_host_intr( struct ata_port *ap,
  501. struct ata_queued_cmd *qc)
  502. {
  503. unsigned int handled = 0;
  504. u32 tmp;
  505. void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr + PDC_GLOBAL_CTL;
  506. tmp = readl(mmio);
  507. if (tmp & PDC_ERR_MASK) {
  508. qc->err_mask |= AC_ERR_DEV;
  509. pdc_reset_port(ap);
  510. }
  511. switch (qc->tf.protocol) {
  512. case ATA_PROT_DMA:
  513. case ATA_PROT_NODATA:
  514. case ATA_PROT_ATAPI_DMA:
  515. qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
  516. ata_qc_complete(qc);
  517. handled = 1;
  518. break;
  519. default:
  520. ap->stats.idle_irq++;
  521. break;
  522. }
  523. return handled;
  524. }
  525. static void pdc_irq_clear(struct ata_port *ap)
  526. {
  527. struct ata_host *host = ap->host;
  528. void __iomem *mmio = host->mmio_base;
  529. readl(mmio + PDC_INT_SEQMASK);
  530. }
  531. static irqreturn_t pdc_interrupt (int irq, void *dev_instance)
  532. {
  533. struct ata_host *host = dev_instance;
  534. struct ata_port *ap;
  535. u32 mask = 0;
  536. unsigned int i, tmp;
  537. unsigned int handled = 0;
  538. void __iomem *mmio_base;
  539. VPRINTK("ENTER\n");
  540. if (!host || !host->mmio_base) {
  541. VPRINTK("QUICK EXIT\n");
  542. return IRQ_NONE;
  543. }
  544. mmio_base = host->mmio_base;
  545. /* reading should also clear interrupts */
  546. mask = readl(mmio_base + PDC_INT_SEQMASK);
  547. if (mask == 0xffffffff) {
  548. VPRINTK("QUICK EXIT 2\n");
  549. return IRQ_NONE;
  550. }
  551. spin_lock(&host->lock);
  552. mask &= 0xffff; /* only 16 tags possible */
  553. if (!mask) {
  554. VPRINTK("QUICK EXIT 3\n");
  555. goto done_irq;
  556. }
  557. writel(mask, mmio_base + PDC_INT_SEQMASK);
  558. for (i = 0; i < host->n_ports; i++) {
  559. VPRINTK("port %u\n", i);
  560. ap = host->ports[i];
  561. tmp = mask & (1 << (i + 1));
  562. if (tmp && ap &&
  563. !(ap->flags & ATA_FLAG_DISABLED)) {
  564. struct ata_queued_cmd *qc;
  565. qc = ata_qc_from_tag(ap, ap->active_tag);
  566. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
  567. handled += pdc_host_intr(ap, qc);
  568. }
  569. }
  570. VPRINTK("EXIT\n");
  571. done_irq:
  572. spin_unlock(&host->lock);
  573. return IRQ_RETVAL(handled);
  574. }
  575. static inline void pdc_packet_start(struct ata_queued_cmd *qc)
  576. {
  577. struct ata_port *ap = qc->ap;
  578. struct pdc_port_priv *pp = ap->private_data;
  579. unsigned int port_no = ap->port_no;
  580. u8 seq = (u8) (port_no + 1);
  581. VPRINTK("ENTER, ap %p\n", ap);
  582. writel(0x00000001, ap->host->mmio_base + (seq * 4));
  583. readl(ap->host->mmio_base + (seq * 4)); /* flush */
  584. pp->pkt[2] = seq;
  585. wmb(); /* flush PRD, pkt writes */
  586. writel(pp->pkt_dma, (void __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  587. readl((void __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT); /* flush */
  588. }
  589. static unsigned int pdc_wait_for_drq(struct ata_port *ap)
  590. {
  591. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  592. unsigned int i;
  593. unsigned int status;
  594. /* Following pdc-ultra's WaitForDrq() we loop here until BSY
  595. * is clear and DRQ is set in altstatus. We could possibly call
  596. * ata_busy_wait() and loop until DRQ is set, but since we don't
  597. * know how much time a call to ata_busy_wait() took, we don't
  598. * know when to time out the outer loop.
  599. */
  600. for(i = 0; i < 1000; ++i) {
  601. status = readb(port_mmio + PDC_ALTSTATUS);
  602. if (status == 0xFF)
  603. break;
  604. if (status & ATA_BUSY)
  605. ;
  606. else if (status & (ATA_DRQ | ATA_ERR))
  607. break;
  608. mdelay(1);
  609. }
  610. if (i >= 1000)
  611. ata_port_printk(ap, KERN_WARNING, "%s timed out\n", __FUNCTION__);
  612. return status;
  613. }
  614. static unsigned int pdc_wait_on_busy(struct ata_port *ap)
  615. {
  616. unsigned int status = ata_busy_wait(ap, ATA_BUSY, 1000);
  617. if (status != 0xff && (status & ATA_BUSY))
  618. ata_port_printk(ap, KERN_WARNING, "%s timed out\n", __FUNCTION__);
  619. return status;
  620. }
  621. static void pdc_issue_atapi_pkt_cmd(struct ata_queued_cmd *qc)
  622. {
  623. struct ata_port *ap = qc->ap;
  624. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  625. void __iomem *host_mmio = ap->host->mmio_base;
  626. unsigned int nbytes;
  627. unsigned int tmp;
  628. writeb(0x00, port_mmio + PDC_CTLSTAT); /* route drive INT to SEQ 0 */
  629. writeb(PDC_SEQCNTRL_INT_MASK, host_mmio + 0); /* but mask SEQ 0 INT */
  630. /* select drive */
  631. if (sata_scr_valid(ap)) {
  632. tmp = PDC_DEVICE_SATA;
  633. } else {
  634. tmp = ATA_DEVICE_OBS;
  635. if (qc->dev->devno != 0)
  636. tmp |= ATA_DEV1;
  637. }
  638. writeb(tmp, port_mmio + PDC_DEVICE);
  639. pdc_wait_on_busy(ap);
  640. writeb(0x00, port_mmio + PDC_SECTOR_COUNT);
  641. writeb(0x00, port_mmio + PDC_SECTOR_NUMBER);
  642. /* set feature and byte counter registers */
  643. if (qc->tf.protocol != ATA_PROT_ATAPI_DMA) {
  644. tmp = PDC_FEATURE_ATAPI_PIO;
  645. /* set byte counter register to real transfer byte count */
  646. nbytes = qc->nbytes;
  647. if (!nbytes)
  648. nbytes = qc->nsect << 9;
  649. if (nbytes > 0xffff)
  650. nbytes = 0xffff;
  651. } else {
  652. tmp = PDC_FEATURE_ATAPI_DMA;
  653. /* set byte counter register to 0 */
  654. nbytes = 0;
  655. }
  656. writeb(tmp, port_mmio + PDC_FEATURE);
  657. writeb(nbytes & 0xFF, port_mmio + PDC_CYLINDER_LOW);
  658. writeb((nbytes >> 8) & 0xFF, port_mmio + PDC_CYLINDER_HIGH);
  659. /* send ATAPI packet command 0xA0 */
  660. writeb(ATA_CMD_PACKET, port_mmio + PDC_COMMAND);
  661. /* pdc_qc_issue_prot() currently sends ATAPI PIO packets back
  662. * to libata. If we start handling those packets ourselves,
  663. * then we must busy-wait for INT (CTLSTAT bit 27) at this point
  664. * if the device has ATA_DFLAG_CDB_INTR set.
  665. */
  666. pdc_wait_for_drq(ap);
  667. /* now the device only waits for the CDB */
  668. }
  669. static unsigned int pdc_qc_issue_prot(struct ata_queued_cmd *qc)
  670. {
  671. switch (qc->tf.protocol) {
  672. case ATA_PROT_ATAPI_DMA:
  673. pdc_issue_atapi_pkt_cmd(qc);
  674. /*FALLTHROUGH*/
  675. case ATA_PROT_DMA:
  676. case ATA_PROT_NODATA:
  677. pdc_packet_start(qc);
  678. return 0;
  679. default:
  680. break;
  681. }
  682. return ata_qc_issue_prot(qc);
  683. }
  684. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  685. {
  686. WARN_ON (tf->protocol == ATA_PROT_DMA ||
  687. tf->protocol == ATA_PROT_NODATA);
  688. ata_tf_load(ap, tf);
  689. }
  690. static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  691. {
  692. WARN_ON (tf->protocol == ATA_PROT_DMA ||
  693. tf->protocol == ATA_PROT_NODATA);
  694. ata_exec_command(ap, tf);
  695. }
  696. static int pdc_check_atapi_dma(struct ata_queued_cmd *qc)
  697. {
  698. u8 *scsicmd = qc->scsicmd->cmnd;
  699. int pio = 1; /* atapi dma off by default */
  700. /* Whitelist commands that may use DMA. */
  701. switch (scsicmd[0]) {
  702. case WRITE_12:
  703. case WRITE_10:
  704. case WRITE_6:
  705. case READ_12:
  706. case READ_10:
  707. case READ_6:
  708. case 0xad: /* READ_DVD_STRUCTURE */
  709. case 0xbe: /* READ_CD */
  710. pio = 0;
  711. }
  712. /* -45150 (FFFF4FA2) to -1 (FFFFFFFF) shall use PIO mode */
  713. if (scsicmd[0] == WRITE_10) {
  714. unsigned int lba;
  715. lba = (scsicmd[2] << 24) | (scsicmd[3] << 16) | (scsicmd[4] << 8) | scsicmd[5];
  716. if (lba >= 0xFFFF4FA2)
  717. pio = 1;
  718. }
  719. return pio;
  720. }
  721. static int pdc_old_check_atapi_dma(struct ata_queued_cmd *qc)
  722. {
  723. struct ata_port *ap = qc->ap;
  724. /* First generation chips cannot use ATAPI DMA on SATA ports */
  725. if (sata_scr_valid(ap))
  726. return 1;
  727. return pdc_check_atapi_dma(qc);
  728. }
  729. static void pdc_ata_setup_port(struct ata_ioports *port, unsigned long base)
  730. {
  731. port->cmd_addr = base;
  732. port->data_addr = base;
  733. port->feature_addr =
  734. port->error_addr = base + 0x4;
  735. port->nsect_addr = base + 0x8;
  736. port->lbal_addr = base + 0xc;
  737. port->lbam_addr = base + 0x10;
  738. port->lbah_addr = base + 0x14;
  739. port->device_addr = base + 0x18;
  740. port->command_addr =
  741. port->status_addr = base + 0x1c;
  742. port->altstatus_addr =
  743. port->ctl_addr = base + 0x38;
  744. }
  745. static void pdc_host_init(unsigned int chip_id, struct ata_probe_ent *pe)
  746. {
  747. void __iomem *mmio = pe->mmio_base;
  748. struct pdc_host_priv *hp = pe->private_data;
  749. int hotplug_offset;
  750. u32 tmp;
  751. if (hp->flags & PDC_FLAG_GEN_II)
  752. hotplug_offset = PDC2_SATA_PLUG_CSR;
  753. else
  754. hotplug_offset = PDC_SATA_PLUG_CSR;
  755. /*
  756. * Except for the hotplug stuff, this is voodoo from the
  757. * Promise driver. Label this entire section
  758. * "TODO: figure out why we do this"
  759. */
  760. /* enable BMR_BURST, maybe change FIFO_SHD to 8 dwords */
  761. tmp = readl(mmio + PDC_FLASH_CTL);
  762. tmp |= 0x02000; /* bit 13 (enable bmr burst) */
  763. if (!(hp->flags & PDC_FLAG_GEN_II))
  764. tmp |= 0x10000; /* bit 16 (fifo threshold at 8 dw) */
  765. writel(tmp, mmio + PDC_FLASH_CTL);
  766. /* clear plug/unplug flags for all ports */
  767. tmp = readl(mmio + hotplug_offset);
  768. writel(tmp | 0xff, mmio + hotplug_offset);
  769. /* mask plug/unplug ints */
  770. tmp = readl(mmio + hotplug_offset);
  771. writel(tmp | 0xff0000, mmio + hotplug_offset);
  772. /* don't initialise TBG or SLEW on 2nd generation chips */
  773. if (hp->flags & PDC_FLAG_GEN_II)
  774. return;
  775. /* reduce TBG clock to 133 Mhz. */
  776. tmp = readl(mmio + PDC_TBG_MODE);
  777. tmp &= ~0x30000; /* clear bit 17, 16*/
  778. tmp |= 0x10000; /* set bit 17:16 = 0:1 */
  779. writel(tmp, mmio + PDC_TBG_MODE);
  780. readl(mmio + PDC_TBG_MODE); /* flush */
  781. msleep(10);
  782. /* adjust slew rate control register. */
  783. tmp = readl(mmio + PDC_SLEW_CTL);
  784. tmp &= 0xFFFFF03F; /* clear bit 11 ~ 6 */
  785. tmp |= 0x00000900; /* set bit 11-9 = 100b , bit 8-6 = 100 */
  786. writel(tmp, mmio + PDC_SLEW_CTL);
  787. }
  788. static int pdc_ata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  789. {
  790. static int printed_version;
  791. struct ata_probe_ent *probe_ent = NULL;
  792. struct pdc_host_priv *hp;
  793. unsigned long base;
  794. void __iomem *mmio_base;
  795. unsigned int board_idx = (unsigned int) ent->driver_data;
  796. int pci_dev_busy = 0;
  797. int rc;
  798. u8 tmp;
  799. if (!printed_version++)
  800. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  801. rc = pci_enable_device(pdev);
  802. if (rc)
  803. return rc;
  804. rc = pci_request_regions(pdev, DRV_NAME);
  805. if (rc) {
  806. pci_dev_busy = 1;
  807. goto err_out;
  808. }
  809. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  810. if (rc)
  811. goto err_out_regions;
  812. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  813. if (rc)
  814. goto err_out_regions;
  815. probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
  816. if (probe_ent == NULL) {
  817. rc = -ENOMEM;
  818. goto err_out_regions;
  819. }
  820. probe_ent->dev = pci_dev_to_dev(pdev);
  821. INIT_LIST_HEAD(&probe_ent->node);
  822. mmio_base = pci_iomap(pdev, 3, 0);
  823. if (mmio_base == NULL) {
  824. rc = -ENOMEM;
  825. goto err_out_free_ent;
  826. }
  827. base = (unsigned long) mmio_base;
  828. hp = kzalloc(sizeof(*hp), GFP_KERNEL);
  829. if (hp == NULL) {
  830. rc = -ENOMEM;
  831. goto err_out_free_ent;
  832. }
  833. probe_ent->private_data = hp;
  834. probe_ent->sht = pdc_port_info[board_idx].sht;
  835. probe_ent->port_flags = pdc_port_info[board_idx].flags;
  836. probe_ent->pio_mask = pdc_port_info[board_idx].pio_mask;
  837. probe_ent->mwdma_mask = pdc_port_info[board_idx].mwdma_mask;
  838. probe_ent->udma_mask = pdc_port_info[board_idx].udma_mask;
  839. probe_ent->port_ops = pdc_port_info[board_idx].port_ops;
  840. probe_ent->irq = pdev->irq;
  841. probe_ent->irq_flags = IRQF_SHARED;
  842. probe_ent->mmio_base = mmio_base;
  843. pdc_ata_setup_port(&probe_ent->port[0], base + 0x200);
  844. pdc_ata_setup_port(&probe_ent->port[1], base + 0x280);
  845. probe_ent->port[0].scr_addr = base + 0x400;
  846. probe_ent->port[1].scr_addr = base + 0x500;
  847. /* notice 4-port boards */
  848. switch (board_idx) {
  849. case board_40518:
  850. hp->flags |= PDC_FLAG_GEN_II;
  851. /* Fall through */
  852. case board_20319:
  853. probe_ent->n_ports = 4;
  854. pdc_ata_setup_port(&probe_ent->port[2], base + 0x300);
  855. pdc_ata_setup_port(&probe_ent->port[3], base + 0x380);
  856. probe_ent->port[2].scr_addr = base + 0x600;
  857. probe_ent->port[3].scr_addr = base + 0x700;
  858. break;
  859. case board_2057x:
  860. hp->flags |= PDC_FLAG_GEN_II;
  861. /* Fall through */
  862. case board_2037x:
  863. /* TX2plus boards also have a PATA port */
  864. tmp = readb(mmio_base + PDC_FLASH_CTL+1);
  865. if (!(tmp & 0x80)) {
  866. probe_ent->n_ports = 3;
  867. pdc_ata_setup_port(&probe_ent->port[2], base + 0x300);
  868. hp->port_flags[2] = ATA_FLAG_SLAVE_POSS;
  869. printk(KERN_INFO DRV_NAME " PATA port found\n");
  870. } else
  871. probe_ent->n_ports = 2;
  872. hp->port_flags[0] = ATA_FLAG_SATA;
  873. hp->port_flags[1] = ATA_FLAG_SATA;
  874. break;
  875. case board_20619:
  876. probe_ent->n_ports = 4;
  877. pdc_ata_setup_port(&probe_ent->port[2], base + 0x300);
  878. pdc_ata_setup_port(&probe_ent->port[3], base + 0x380);
  879. probe_ent->port[2].scr_addr = base + 0x600;
  880. probe_ent->port[3].scr_addr = base + 0x700;
  881. break;
  882. default:
  883. BUG();
  884. break;
  885. }
  886. pci_set_master(pdev);
  887. /* initialize adapter */
  888. pdc_host_init(board_idx, probe_ent);
  889. /* FIXME: Need any other frees than hp? */
  890. if (!ata_device_add(probe_ent))
  891. kfree(hp);
  892. kfree(probe_ent);
  893. return 0;
  894. err_out_free_ent:
  895. kfree(probe_ent);
  896. err_out_regions:
  897. pci_release_regions(pdev);
  898. err_out:
  899. if (!pci_dev_busy)
  900. pci_disable_device(pdev);
  901. return rc;
  902. }
  903. static int __init pdc_ata_init(void)
  904. {
  905. return pci_register_driver(&pdc_ata_pci_driver);
  906. }
  907. static void __exit pdc_ata_exit(void)
  908. {
  909. pci_unregister_driver(&pdc_ata_pci_driver);
  910. }
  911. MODULE_AUTHOR("Jeff Garzik");
  912. MODULE_DESCRIPTION("Promise ATA TX2/TX4/TX4000 low-level driver");
  913. MODULE_LICENSE("GPL");
  914. MODULE_DEVICE_TABLE(pci, pdc_ata_pci_tbl);
  915. MODULE_VERSION(DRV_VERSION);
  916. module_init(pdc_ata_init);
  917. module_exit(pdc_ata_exit);