sata_promise.c 27 KB

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