sata_promise.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. /*
  2. * sata_promise.c - Promise SATA
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Mikael Pettersson <mikpe@it.uu.se>
  6. * Please ALWAYS copy linux-ide@vger.kernel.org
  7. * on emails.
  8. *
  9. * Copyright 2003-2004 Red Hat, Inc.
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * libata documentation is available via 'make {ps|pdf}docs',
  28. * as Documentation/DocBook/libata.*
  29. *
  30. * Hardware information only available under NDA.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/pci.h>
  36. #include <linux/init.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/delay.h>
  39. #include <linux/interrupt.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 "sata_promise.h"
  46. #define DRV_NAME "sata_promise"
  47. #define DRV_VERSION "2.12"
  48. enum {
  49. PDC_MAX_PORTS = 4,
  50. PDC_MMIO_BAR = 3,
  51. PDC_MAX_PRD = LIBATA_MAX_PRD - 1, /* -1 for ASIC PRD bug workaround */
  52. /* register offsets */
  53. PDC_FEATURE = 0x04, /* Feature/Error reg (per port) */
  54. PDC_SECTOR_COUNT = 0x08, /* Sector count reg (per port) */
  55. PDC_SECTOR_NUMBER = 0x0C, /* Sector number reg (per port) */
  56. PDC_CYLINDER_LOW = 0x10, /* Cylinder low reg (per port) */
  57. PDC_CYLINDER_HIGH = 0x14, /* Cylinder high reg (per port) */
  58. PDC_DEVICE = 0x18, /* Device/Head reg (per port) */
  59. PDC_COMMAND = 0x1C, /* Command/status reg (per port) */
  60. PDC_ALTSTATUS = 0x38, /* Alternate-status/device-control reg (per port) */
  61. PDC_PKT_SUBMIT = 0x40, /* Command packet pointer addr */
  62. PDC_INT_SEQMASK = 0x40, /* Mask of asserted SEQ INTs */
  63. PDC_FLASH_CTL = 0x44, /* Flash control register */
  64. PDC_GLOBAL_CTL = 0x48, /* Global control/status (per port) */
  65. PDC_CTLSTAT = 0x60, /* IDE control and status (per port) */
  66. PDC_SATA_PLUG_CSR = 0x6C, /* SATA Plug control/status reg */
  67. PDC2_SATA_PLUG_CSR = 0x60, /* SATAII Plug control/status reg */
  68. PDC_TBG_MODE = 0x41C, /* TBG mode (not SATAII) */
  69. PDC_SLEW_CTL = 0x470, /* slew rate control reg (not SATAII) */
  70. /* PDC_GLOBAL_CTL bit definitions */
  71. PDC_PH_ERR = (1 << 8), /* PCI error while loading packet */
  72. PDC_SH_ERR = (1 << 9), /* PCI error while loading S/G table */
  73. PDC_DH_ERR = (1 << 10), /* PCI error while loading data */
  74. PDC2_HTO_ERR = (1 << 12), /* host bus timeout */
  75. PDC2_ATA_HBA_ERR = (1 << 13), /* error during SATA DATA FIS transmission */
  76. PDC2_ATA_DMA_CNT_ERR = (1 << 14), /* DMA DATA FIS size differs from S/G count */
  77. PDC_OVERRUN_ERR = (1 << 19), /* S/G byte count larger than HD requires */
  78. PDC_UNDERRUN_ERR = (1 << 20), /* S/G byte count less than HD requires */
  79. PDC_DRIVE_ERR = (1 << 21), /* drive error */
  80. PDC_PCI_SYS_ERR = (1 << 22), /* PCI system error */
  81. PDC1_PCI_PARITY_ERR = (1 << 23), /* PCI parity error (from SATA150 driver) */
  82. PDC1_ERR_MASK = PDC1_PCI_PARITY_ERR,
  83. PDC2_ERR_MASK = PDC2_HTO_ERR | PDC2_ATA_HBA_ERR |
  84. PDC2_ATA_DMA_CNT_ERR,
  85. PDC_ERR_MASK = PDC_PH_ERR | PDC_SH_ERR | PDC_DH_ERR |
  86. PDC_OVERRUN_ERR | PDC_UNDERRUN_ERR |
  87. PDC_DRIVE_ERR | PDC_PCI_SYS_ERR |
  88. PDC1_ERR_MASK | PDC2_ERR_MASK,
  89. board_2037x = 0, /* FastTrak S150 TX2plus */
  90. board_2037x_pata = 1, /* FastTrak S150 TX2plus PATA port */
  91. board_20319 = 2, /* FastTrak S150 TX4 */
  92. board_20619 = 3, /* FastTrak TX4000 */
  93. board_2057x = 4, /* SATAII150 Tx2plus */
  94. board_2057x_pata = 5, /* SATAII150 Tx2plus PATA port */
  95. board_40518 = 6, /* SATAII150 Tx4 */
  96. PDC_HAS_PATA = (1 << 1), /* PDC20375/20575 has PATA */
  97. /* Sequence counter control registers bit definitions */
  98. PDC_SEQCNTRL_INT_MASK = (1 << 5), /* Sequence Interrupt Mask */
  99. /* Feature register values */
  100. PDC_FEATURE_ATAPI_PIO = 0x00, /* ATAPI data xfer by PIO */
  101. PDC_FEATURE_ATAPI_DMA = 0x01, /* ATAPI data xfer by DMA */
  102. /* Device/Head register values */
  103. PDC_DEVICE_SATA = 0xE0, /* Device/Head value for SATA devices */
  104. /* PDC_CTLSTAT bit definitions */
  105. PDC_DMA_ENABLE = (1 << 7),
  106. PDC_IRQ_DISABLE = (1 << 10),
  107. PDC_RESET = (1 << 11), /* HDMA reset */
  108. PDC_COMMON_FLAGS = ATA_FLAG_NO_LEGACY |
  109. ATA_FLAG_MMIO |
  110. ATA_FLAG_PIO_POLLING,
  111. /* ap->flags bits */
  112. PDC_FLAG_GEN_II = (1 << 24),
  113. PDC_FLAG_SATA_PATA = (1 << 25), /* supports SATA + PATA */
  114. PDC_FLAG_4_PORTS = (1 << 26), /* 4 ports */
  115. };
  116. struct pdc_port_priv {
  117. u8 *pkt;
  118. dma_addr_t pkt_dma;
  119. };
  120. static int pdc_sata_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val);
  121. static int pdc_sata_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val);
  122. static int pdc_ata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  123. static int pdc_common_port_start(struct ata_port *ap);
  124. static int pdc_sata_port_start(struct ata_port *ap);
  125. static void pdc_qc_prep(struct ata_queued_cmd *qc);
  126. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf);
  127. static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf);
  128. static int pdc_check_atapi_dma(struct ata_queued_cmd *qc);
  129. static int pdc_old_sata_check_atapi_dma(struct ata_queued_cmd *qc);
  130. static void pdc_irq_clear(struct ata_port *ap);
  131. static unsigned int pdc_qc_issue_prot(struct ata_queued_cmd *qc);
  132. static void pdc_freeze(struct ata_port *ap);
  133. static void pdc_sata_freeze(struct ata_port *ap);
  134. static void pdc_thaw(struct ata_port *ap);
  135. static void pdc_sata_thaw(struct ata_port *ap);
  136. static void pdc_pata_error_handler(struct ata_port *ap);
  137. static void pdc_sata_error_handler(struct ata_port *ap);
  138. static void pdc_post_internal_cmd(struct ata_queued_cmd *qc);
  139. static int pdc_pata_cable_detect(struct ata_port *ap);
  140. static int pdc_sata_cable_detect(struct ata_port *ap);
  141. static struct scsi_host_template pdc_ata_sht = {
  142. .module = THIS_MODULE,
  143. .name = DRV_NAME,
  144. .ioctl = ata_scsi_ioctl,
  145. .queuecommand = ata_scsi_queuecmd,
  146. .can_queue = ATA_DEF_QUEUE,
  147. .this_id = ATA_SHT_THIS_ID,
  148. .sg_tablesize = PDC_MAX_PRD,
  149. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  150. .emulated = ATA_SHT_EMULATED,
  151. .use_clustering = ATA_SHT_USE_CLUSTERING,
  152. .proc_name = DRV_NAME,
  153. .dma_boundary = ATA_DMA_BOUNDARY,
  154. .slave_configure = ata_scsi_slave_config,
  155. .slave_destroy = ata_scsi_slave_destroy,
  156. .bios_param = ata_std_bios_param,
  157. };
  158. static const struct ata_port_operations pdc_sata_ops = {
  159. .tf_load = pdc_tf_load_mmio,
  160. .tf_read = ata_tf_read,
  161. .check_status = ata_check_status,
  162. .exec_command = pdc_exec_command_mmio,
  163. .dev_select = ata_std_dev_select,
  164. .check_atapi_dma = pdc_check_atapi_dma,
  165. .qc_prep = pdc_qc_prep,
  166. .qc_issue = pdc_qc_issue_prot,
  167. .freeze = pdc_sata_freeze,
  168. .thaw = pdc_sata_thaw,
  169. .error_handler = pdc_sata_error_handler,
  170. .post_internal_cmd = pdc_post_internal_cmd,
  171. .cable_detect = pdc_sata_cable_detect,
  172. .data_xfer = ata_data_xfer,
  173. .irq_clear = pdc_irq_clear,
  174. .irq_on = ata_irq_on,
  175. .scr_read = pdc_sata_scr_read,
  176. .scr_write = pdc_sata_scr_write,
  177. .port_start = pdc_sata_port_start,
  178. };
  179. /* First-generation chips need a more restrictive ->check_atapi_dma op */
  180. static const struct ata_port_operations pdc_old_sata_ops = {
  181. .tf_load = pdc_tf_load_mmio,
  182. .tf_read = ata_tf_read,
  183. .check_status = ata_check_status,
  184. .exec_command = pdc_exec_command_mmio,
  185. .dev_select = ata_std_dev_select,
  186. .check_atapi_dma = pdc_old_sata_check_atapi_dma,
  187. .qc_prep = pdc_qc_prep,
  188. .qc_issue = pdc_qc_issue_prot,
  189. .freeze = pdc_sata_freeze,
  190. .thaw = pdc_sata_thaw,
  191. .error_handler = pdc_sata_error_handler,
  192. .post_internal_cmd = pdc_post_internal_cmd,
  193. .cable_detect = pdc_sata_cable_detect,
  194. .data_xfer = ata_data_xfer,
  195. .irq_clear = pdc_irq_clear,
  196. .irq_on = ata_irq_on,
  197. .scr_read = pdc_sata_scr_read,
  198. .scr_write = pdc_sata_scr_write,
  199. .port_start = pdc_sata_port_start,
  200. };
  201. static const struct ata_port_operations pdc_pata_ops = {
  202. .tf_load = pdc_tf_load_mmio,
  203. .tf_read = ata_tf_read,
  204. .check_status = ata_check_status,
  205. .exec_command = pdc_exec_command_mmio,
  206. .dev_select = ata_std_dev_select,
  207. .check_atapi_dma = pdc_check_atapi_dma,
  208. .qc_prep = pdc_qc_prep,
  209. .qc_issue = pdc_qc_issue_prot,
  210. .freeze = pdc_freeze,
  211. .thaw = pdc_thaw,
  212. .error_handler = pdc_pata_error_handler,
  213. .post_internal_cmd = pdc_post_internal_cmd,
  214. .cable_detect = pdc_pata_cable_detect,
  215. .data_xfer = ata_data_xfer,
  216. .irq_clear = pdc_irq_clear,
  217. .irq_on = ata_irq_on,
  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 = ATA_UDMA6,
  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 = ATA_UDMA6,
  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 = ATA_UDMA6,
  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 = ATA_UDMA6,
  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 = ATA_UDMA6,
  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 = ATA_UDMA6,
  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 = ATA_UDMA6,
  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_40518 },
  298. { PCI_VDEVICE(PROMISE, 0x3519), board_40518 },
  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 = 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 = 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 int pdc_sata_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val)
  374. {
  375. if (sc_reg > SCR_CONTROL)
  376. return -EINVAL;
  377. *val = readl(ap->ioaddr.scr_addr + (sc_reg * 4));
  378. return 0;
  379. }
  380. static int pdc_sata_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val)
  381. {
  382. if (sc_reg > SCR_CONTROL)
  383. return -EINVAL;
  384. writel(val, ap->ioaddr.scr_addr + (sc_reg * 4));
  385. return 0;
  386. }
  387. static void pdc_atapi_pkt(struct ata_queued_cmd *qc)
  388. {
  389. struct ata_port *ap = qc->ap;
  390. dma_addr_t sg_table = ap->prd_dma;
  391. unsigned int cdb_len = qc->dev->cdb_len;
  392. u8 *cdb = qc->cdb;
  393. struct pdc_port_priv *pp = ap->private_data;
  394. u8 *buf = pp->pkt;
  395. u32 *buf32 = (u32 *) buf;
  396. unsigned int dev_sel, feature;
  397. /* set control bits (byte 0), zero delay seq id (byte 3),
  398. * and seq id (byte 2)
  399. */
  400. switch (qc->tf.protocol) {
  401. case ATAPI_PROT_DMA:
  402. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  403. buf32[0] = cpu_to_le32(PDC_PKT_READ);
  404. else
  405. buf32[0] = 0;
  406. break;
  407. case ATAPI_PROT_NODATA:
  408. buf32[0] = cpu_to_le32(PDC_PKT_NODATA);
  409. break;
  410. default:
  411. BUG();
  412. break;
  413. }
  414. buf32[1] = cpu_to_le32(sg_table); /* S/G table addr */
  415. buf32[2] = 0; /* no next-packet */
  416. /* select drive */
  417. if (sata_scr_valid(&ap->link))
  418. dev_sel = PDC_DEVICE_SATA;
  419. else
  420. dev_sel = qc->tf.device;
  421. buf[12] = (1 << 5) | ATA_REG_DEVICE;
  422. buf[13] = dev_sel;
  423. buf[14] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_CLEAR_BSY;
  424. buf[15] = dev_sel; /* once more, waiting for BSY to clear */
  425. buf[16] = (1 << 5) | ATA_REG_NSECT;
  426. buf[17] = qc->tf.nsect;
  427. buf[18] = (1 << 5) | ATA_REG_LBAL;
  428. buf[19] = qc->tf.lbal;
  429. /* set feature and byte counter registers */
  430. if (qc->tf.protocol != ATAPI_PROT_DMA)
  431. feature = PDC_FEATURE_ATAPI_PIO;
  432. else
  433. feature = PDC_FEATURE_ATAPI_DMA;
  434. buf[20] = (1 << 5) | ATA_REG_FEATURE;
  435. buf[21] = feature;
  436. buf[22] = (1 << 5) | ATA_REG_BYTEL;
  437. buf[23] = qc->tf.lbam;
  438. buf[24] = (1 << 5) | ATA_REG_BYTEH;
  439. buf[25] = qc->tf.lbah;
  440. /* send ATAPI packet command 0xA0 */
  441. buf[26] = (1 << 5) | ATA_REG_CMD;
  442. buf[27] = qc->tf.command;
  443. /* select drive and check DRQ */
  444. buf[28] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_WAIT_DRDY;
  445. buf[29] = dev_sel;
  446. /* we can represent cdb lengths 2/4/6/8/10/12/14/16 */
  447. BUG_ON(cdb_len & ~0x1E);
  448. /* append the CDB as the final part */
  449. buf[30] = (((cdb_len >> 1) & 7) << 5) | ATA_REG_DATA | PDC_LAST_REG;
  450. memcpy(buf+31, cdb, cdb_len);
  451. }
  452. /**
  453. * pdc_fill_sg - Fill PCI IDE PRD table
  454. * @qc: Metadata associated with taskfile to be transferred
  455. *
  456. * Fill PCI IDE PRD (scatter-gather) table with segments
  457. * associated with the current disk command.
  458. * Make sure hardware does not choke on it.
  459. *
  460. * LOCKING:
  461. * spin_lock_irqsave(host lock)
  462. *
  463. */
  464. static void pdc_fill_sg(struct ata_queued_cmd *qc)
  465. {
  466. struct ata_port *ap = qc->ap;
  467. struct scatterlist *sg;
  468. const u32 SG_COUNT_ASIC_BUG = 41*4;
  469. unsigned int si, idx;
  470. u32 len;
  471. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  472. return;
  473. idx = 0;
  474. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  475. u32 addr, offset;
  476. u32 sg_len;
  477. /* determine if physical DMA addr spans 64K boundary.
  478. * Note h/w doesn't support 64-bit, so we unconditionally
  479. * truncate dma_addr_t to u32.
  480. */
  481. addr = (u32) sg_dma_address(sg);
  482. sg_len = sg_dma_len(sg);
  483. while (sg_len) {
  484. offset = addr & 0xffff;
  485. len = sg_len;
  486. if ((offset + sg_len) > 0x10000)
  487. len = 0x10000 - offset;
  488. ap->prd[idx].addr = cpu_to_le32(addr);
  489. ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
  490. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  491. idx++;
  492. sg_len -= len;
  493. addr += len;
  494. }
  495. }
  496. len = le32_to_cpu(ap->prd[idx - 1].flags_len);
  497. if (len > SG_COUNT_ASIC_BUG) {
  498. u32 addr;
  499. VPRINTK("Splitting last PRD.\n");
  500. addr = le32_to_cpu(ap->prd[idx - 1].addr);
  501. ap->prd[idx - 1].flags_len = cpu_to_le32(len - SG_COUNT_ASIC_BUG);
  502. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx - 1, addr, SG_COUNT_ASIC_BUG);
  503. addr = addr + len - SG_COUNT_ASIC_BUG;
  504. len = SG_COUNT_ASIC_BUG;
  505. ap->prd[idx].addr = cpu_to_le32(addr);
  506. ap->prd[idx].flags_len = cpu_to_le32(len);
  507. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  508. idx++;
  509. }
  510. ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  511. }
  512. static void pdc_qc_prep(struct ata_queued_cmd *qc)
  513. {
  514. struct pdc_port_priv *pp = qc->ap->private_data;
  515. unsigned int i;
  516. VPRINTK("ENTER\n");
  517. switch (qc->tf.protocol) {
  518. case ATA_PROT_DMA:
  519. pdc_fill_sg(qc);
  520. /* fall through */
  521. case ATA_PROT_NODATA:
  522. i = pdc_pkt_header(&qc->tf, qc->ap->prd_dma,
  523. qc->dev->devno, pp->pkt);
  524. if (qc->tf.flags & ATA_TFLAG_LBA48)
  525. i = pdc_prep_lba48(&qc->tf, pp->pkt, i);
  526. else
  527. i = pdc_prep_lba28(&qc->tf, pp->pkt, i);
  528. pdc_pkt_footer(&qc->tf, pp->pkt, i);
  529. break;
  530. case ATAPI_PROT_PIO:
  531. pdc_fill_sg(qc);
  532. break;
  533. case ATAPI_PROT_DMA:
  534. pdc_fill_sg(qc);
  535. /*FALLTHROUGH*/
  536. case ATAPI_PROT_NODATA:
  537. pdc_atapi_pkt(qc);
  538. break;
  539. default:
  540. break;
  541. }
  542. }
  543. static int pdc_is_sataii_tx4(unsigned long flags)
  544. {
  545. const unsigned long mask = PDC_FLAG_GEN_II | PDC_FLAG_4_PORTS;
  546. return (flags & mask) == mask;
  547. }
  548. static unsigned int pdc_port_no_to_ata_no(unsigned int port_no,
  549. int is_sataii_tx4)
  550. {
  551. static const unsigned char sataii_tx4_port_remap[4] = { 3, 1, 0, 2};
  552. return is_sataii_tx4 ? sataii_tx4_port_remap[port_no] : port_no;
  553. }
  554. static unsigned int pdc_sata_nr_ports(const struct ata_port *ap)
  555. {
  556. return (ap->flags & PDC_FLAG_4_PORTS) ? 4 : 2;
  557. }
  558. static unsigned int pdc_sata_ata_port_to_ata_no(const struct ata_port *ap)
  559. {
  560. const struct ata_host *host = ap->host;
  561. unsigned int nr_ports = pdc_sata_nr_ports(ap);
  562. unsigned int i;
  563. for(i = 0; i < nr_ports && host->ports[i] != ap; ++i)
  564. ;
  565. BUG_ON(i >= nr_ports);
  566. return pdc_port_no_to_ata_no(i, pdc_is_sataii_tx4(ap->flags));
  567. }
  568. static unsigned int pdc_sata_hotplug_offset(const struct ata_port *ap)
  569. {
  570. return (ap->flags & PDC_FLAG_GEN_II) ? PDC2_SATA_PLUG_CSR : PDC_SATA_PLUG_CSR;
  571. }
  572. static void pdc_freeze(struct ata_port *ap)
  573. {
  574. void __iomem *mmio = ap->ioaddr.cmd_addr;
  575. u32 tmp;
  576. tmp = readl(mmio + PDC_CTLSTAT);
  577. tmp |= PDC_IRQ_DISABLE;
  578. tmp &= ~PDC_DMA_ENABLE;
  579. writel(tmp, mmio + PDC_CTLSTAT);
  580. readl(mmio + PDC_CTLSTAT); /* flush */
  581. }
  582. static void pdc_sata_freeze(struct ata_port *ap)
  583. {
  584. struct ata_host *host = ap->host;
  585. void __iomem *host_mmio = host->iomap[PDC_MMIO_BAR];
  586. unsigned int hotplug_offset = pdc_sata_hotplug_offset(ap);
  587. unsigned int ata_no = pdc_sata_ata_port_to_ata_no(ap);
  588. u32 hotplug_status;
  589. /* Disable hotplug events on this port.
  590. *
  591. * Locking:
  592. * 1) hotplug register accesses must be serialised via host->lock
  593. * 2) ap->lock == &ap->host->lock
  594. * 3) ->freeze() and ->thaw() are called with ap->lock held
  595. */
  596. hotplug_status = readl(host_mmio + hotplug_offset);
  597. hotplug_status |= 0x11 << (ata_no + 16);
  598. writel(hotplug_status, host_mmio + hotplug_offset);
  599. readl(host_mmio + hotplug_offset); /* flush */
  600. pdc_freeze(ap);
  601. }
  602. static void pdc_thaw(struct ata_port *ap)
  603. {
  604. void __iomem *mmio = ap->ioaddr.cmd_addr;
  605. u32 tmp;
  606. /* clear IRQ */
  607. readl(mmio + PDC_INT_SEQMASK);
  608. /* turn IRQ back on */
  609. tmp = readl(mmio + PDC_CTLSTAT);
  610. tmp &= ~PDC_IRQ_DISABLE;
  611. writel(tmp, mmio + PDC_CTLSTAT);
  612. readl(mmio + PDC_CTLSTAT); /* flush */
  613. }
  614. static void pdc_sata_thaw(struct ata_port *ap)
  615. {
  616. struct ata_host *host = ap->host;
  617. void __iomem *host_mmio = host->iomap[PDC_MMIO_BAR];
  618. unsigned int hotplug_offset = pdc_sata_hotplug_offset(ap);
  619. unsigned int ata_no = pdc_sata_ata_port_to_ata_no(ap);
  620. u32 hotplug_status;
  621. pdc_thaw(ap);
  622. /* Enable hotplug events on this port.
  623. * Locking: see pdc_sata_freeze().
  624. */
  625. hotplug_status = readl(host_mmio + hotplug_offset);
  626. hotplug_status |= 0x11 << ata_no;
  627. hotplug_status &= ~(0x11 << (ata_no + 16));
  628. writel(hotplug_status, host_mmio + hotplug_offset);
  629. readl(host_mmio + hotplug_offset); /* flush */
  630. }
  631. static void pdc_common_error_handler(struct ata_port *ap, ata_reset_fn_t hardreset)
  632. {
  633. if (!(ap->pflags & ATA_PFLAG_FROZEN))
  634. pdc_reset_port(ap);
  635. /* perform recovery */
  636. ata_do_eh(ap, ata_std_prereset, ata_std_softreset, hardreset,
  637. ata_std_postreset);
  638. }
  639. static void pdc_pata_error_handler(struct ata_port *ap)
  640. {
  641. pdc_common_error_handler(ap, NULL);
  642. }
  643. static void pdc_sata_error_handler(struct ata_port *ap)
  644. {
  645. pdc_common_error_handler(ap, sata_std_hardreset);
  646. }
  647. static void pdc_post_internal_cmd(struct ata_queued_cmd *qc)
  648. {
  649. struct ata_port *ap = qc->ap;
  650. /* make DMA engine forget about the failed command */
  651. if (qc->flags & ATA_QCFLAG_FAILED)
  652. pdc_reset_port(ap);
  653. }
  654. static void pdc_error_intr(struct ata_port *ap, struct ata_queued_cmd *qc,
  655. u32 port_status, u32 err_mask)
  656. {
  657. struct ata_eh_info *ehi = &ap->link.eh_info;
  658. unsigned int ac_err_mask = 0;
  659. ata_ehi_clear_desc(ehi);
  660. ata_ehi_push_desc(ehi, "port_status 0x%08x", port_status);
  661. port_status &= err_mask;
  662. if (port_status & PDC_DRIVE_ERR)
  663. ac_err_mask |= AC_ERR_DEV;
  664. if (port_status & (PDC_OVERRUN_ERR | PDC_UNDERRUN_ERR))
  665. ac_err_mask |= AC_ERR_HSM;
  666. if (port_status & (PDC2_ATA_HBA_ERR | PDC2_ATA_DMA_CNT_ERR))
  667. ac_err_mask |= AC_ERR_ATA_BUS;
  668. if (port_status & (PDC_PH_ERR | PDC_SH_ERR | PDC_DH_ERR | PDC2_HTO_ERR
  669. | PDC_PCI_SYS_ERR | PDC1_PCI_PARITY_ERR))
  670. ac_err_mask |= AC_ERR_HOST_BUS;
  671. if (sata_scr_valid(&ap->link)) {
  672. u32 serror;
  673. pdc_sata_scr_read(ap, SCR_ERROR, &serror);
  674. ehi->serror |= serror;
  675. }
  676. qc->err_mask |= ac_err_mask;
  677. pdc_reset_port(ap);
  678. ata_port_abort(ap);
  679. }
  680. static inline unsigned int pdc_host_intr(struct ata_port *ap,
  681. struct ata_queued_cmd *qc)
  682. {
  683. unsigned int handled = 0;
  684. void __iomem *port_mmio = ap->ioaddr.cmd_addr;
  685. u32 port_status, err_mask;
  686. err_mask = PDC_ERR_MASK;
  687. if (ap->flags & PDC_FLAG_GEN_II)
  688. err_mask &= ~PDC1_ERR_MASK;
  689. else
  690. err_mask &= ~PDC2_ERR_MASK;
  691. port_status = readl(port_mmio + PDC_GLOBAL_CTL);
  692. if (unlikely(port_status & err_mask)) {
  693. pdc_error_intr(ap, qc, port_status, err_mask);
  694. return 1;
  695. }
  696. switch (qc->tf.protocol) {
  697. case ATA_PROT_DMA:
  698. case ATA_PROT_NODATA:
  699. case ATAPI_PROT_DMA:
  700. case ATAPI_PROT_NODATA:
  701. qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
  702. ata_qc_complete(qc);
  703. handled = 1;
  704. break;
  705. default:
  706. ap->stats.idle_irq++;
  707. break;
  708. }
  709. return handled;
  710. }
  711. static void pdc_irq_clear(struct ata_port *ap)
  712. {
  713. struct ata_host *host = ap->host;
  714. void __iomem *mmio = host->iomap[PDC_MMIO_BAR];
  715. readl(mmio + PDC_INT_SEQMASK);
  716. }
  717. static irqreturn_t pdc_interrupt(int irq, void *dev_instance)
  718. {
  719. struct ata_host *host = dev_instance;
  720. struct ata_port *ap;
  721. u32 mask = 0;
  722. unsigned int i, tmp;
  723. unsigned int handled = 0;
  724. void __iomem *mmio_base;
  725. unsigned int hotplug_offset, ata_no;
  726. u32 hotplug_status;
  727. int is_sataii_tx4;
  728. VPRINTK("ENTER\n");
  729. if (!host || !host->iomap[PDC_MMIO_BAR]) {
  730. VPRINTK("QUICK EXIT\n");
  731. return IRQ_NONE;
  732. }
  733. mmio_base = host->iomap[PDC_MMIO_BAR];
  734. spin_lock(&host->lock);
  735. /* read and clear hotplug flags for all ports */
  736. if (host->ports[0]->flags & PDC_FLAG_GEN_II)
  737. hotplug_offset = PDC2_SATA_PLUG_CSR;
  738. else
  739. hotplug_offset = PDC_SATA_PLUG_CSR;
  740. hotplug_status = readl(mmio_base + hotplug_offset);
  741. if (hotplug_status & 0xff)
  742. writel(hotplug_status | 0xff, mmio_base + hotplug_offset);
  743. hotplug_status &= 0xff; /* clear uninteresting bits */
  744. /* reading should also clear interrupts */
  745. mask = readl(mmio_base + PDC_INT_SEQMASK);
  746. if (mask == 0xffffffff && hotplug_status == 0) {
  747. VPRINTK("QUICK EXIT 2\n");
  748. goto done_irq;
  749. }
  750. mask &= 0xffff; /* only 16 tags possible */
  751. if (mask == 0 && hotplug_status == 0) {
  752. VPRINTK("QUICK EXIT 3\n");
  753. goto done_irq;
  754. }
  755. writel(mask, mmio_base + PDC_INT_SEQMASK);
  756. is_sataii_tx4 = pdc_is_sataii_tx4(host->ports[0]->flags);
  757. for (i = 0; i < host->n_ports; i++) {
  758. VPRINTK("port %u\n", i);
  759. ap = host->ports[i];
  760. /* check for a plug or unplug event */
  761. ata_no = pdc_port_no_to_ata_no(i, is_sataii_tx4);
  762. tmp = hotplug_status & (0x11 << ata_no);
  763. if (tmp && ap &&
  764. !(ap->flags & ATA_FLAG_DISABLED)) {
  765. struct ata_eh_info *ehi = &ap->link.eh_info;
  766. ata_ehi_clear_desc(ehi);
  767. ata_ehi_hotplugged(ehi);
  768. ata_ehi_push_desc(ehi, "hotplug_status %#x", tmp);
  769. ata_port_freeze(ap);
  770. ++handled;
  771. continue;
  772. }
  773. /* check for a packet interrupt */
  774. tmp = mask & (1 << (i + 1));
  775. if (tmp && ap &&
  776. !(ap->flags & ATA_FLAG_DISABLED)) {
  777. struct ata_queued_cmd *qc;
  778. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  779. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
  780. handled += pdc_host_intr(ap, qc);
  781. }
  782. }
  783. VPRINTK("EXIT\n");
  784. done_irq:
  785. spin_unlock(&host->lock);
  786. return IRQ_RETVAL(handled);
  787. }
  788. static inline void pdc_packet_start(struct ata_queued_cmd *qc)
  789. {
  790. struct ata_port *ap = qc->ap;
  791. struct pdc_port_priv *pp = ap->private_data;
  792. void __iomem *mmio = ap->host->iomap[PDC_MMIO_BAR];
  793. unsigned int port_no = ap->port_no;
  794. u8 seq = (u8) (port_no + 1);
  795. VPRINTK("ENTER, ap %p\n", ap);
  796. writel(0x00000001, mmio + (seq * 4));
  797. readl(mmio + (seq * 4)); /* flush */
  798. pp->pkt[2] = seq;
  799. wmb(); /* flush PRD, pkt writes */
  800. writel(pp->pkt_dma, ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  801. readl(ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT); /* flush */
  802. }
  803. static unsigned int pdc_qc_issue_prot(struct ata_queued_cmd *qc)
  804. {
  805. switch (qc->tf.protocol) {
  806. case ATAPI_PROT_NODATA:
  807. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  808. break;
  809. /*FALLTHROUGH*/
  810. case ATA_PROT_NODATA:
  811. if (qc->tf.flags & ATA_TFLAG_POLLING)
  812. break;
  813. /*FALLTHROUGH*/
  814. case ATAPI_PROT_DMA:
  815. case ATA_PROT_DMA:
  816. pdc_packet_start(qc);
  817. return 0;
  818. default:
  819. break;
  820. }
  821. return ata_qc_issue_prot(qc);
  822. }
  823. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  824. {
  825. WARN_ON(tf->protocol == ATA_PROT_DMA || tf->protocol == ATAPI_PROT_DMA);
  826. ata_tf_load(ap, tf);
  827. }
  828. static void pdc_exec_command_mmio(struct ata_port *ap,
  829. const struct ata_taskfile *tf)
  830. {
  831. WARN_ON(tf->protocol == ATA_PROT_DMA || tf->protocol == ATAPI_PROT_DMA);
  832. ata_exec_command(ap, tf);
  833. }
  834. static int pdc_check_atapi_dma(struct ata_queued_cmd *qc)
  835. {
  836. u8 *scsicmd = qc->scsicmd->cmnd;
  837. int pio = 1; /* atapi dma off by default */
  838. /* Whitelist commands that may use DMA. */
  839. switch (scsicmd[0]) {
  840. case WRITE_12:
  841. case WRITE_10:
  842. case WRITE_6:
  843. case READ_12:
  844. case READ_10:
  845. case READ_6:
  846. case 0xad: /* READ_DVD_STRUCTURE */
  847. case 0xbe: /* READ_CD */
  848. pio = 0;
  849. }
  850. /* -45150 (FFFF4FA2) to -1 (FFFFFFFF) shall use PIO mode */
  851. if (scsicmd[0] == WRITE_10) {
  852. unsigned int lba =
  853. (scsicmd[2] << 24) |
  854. (scsicmd[3] << 16) |
  855. (scsicmd[4] << 8) |
  856. scsicmd[5];
  857. if (lba >= 0xFFFF4FA2)
  858. pio = 1;
  859. }
  860. return pio;
  861. }
  862. static int pdc_old_sata_check_atapi_dma(struct ata_queued_cmd *qc)
  863. {
  864. /* First generation chips cannot use ATAPI DMA on SATA ports */
  865. return 1;
  866. }
  867. static void pdc_ata_setup_port(struct ata_port *ap,
  868. void __iomem *base, void __iomem *scr_addr)
  869. {
  870. ap->ioaddr.cmd_addr = base;
  871. ap->ioaddr.data_addr = base;
  872. ap->ioaddr.feature_addr =
  873. ap->ioaddr.error_addr = base + 0x4;
  874. ap->ioaddr.nsect_addr = base + 0x8;
  875. ap->ioaddr.lbal_addr = base + 0xc;
  876. ap->ioaddr.lbam_addr = base + 0x10;
  877. ap->ioaddr.lbah_addr = base + 0x14;
  878. ap->ioaddr.device_addr = base + 0x18;
  879. ap->ioaddr.command_addr =
  880. ap->ioaddr.status_addr = base + 0x1c;
  881. ap->ioaddr.altstatus_addr =
  882. ap->ioaddr.ctl_addr = base + 0x38;
  883. ap->ioaddr.scr_addr = scr_addr;
  884. }
  885. static void pdc_host_init(struct ata_host *host)
  886. {
  887. void __iomem *mmio = host->iomap[PDC_MMIO_BAR];
  888. int is_gen2 = host->ports[0]->flags & PDC_FLAG_GEN_II;
  889. int hotplug_offset;
  890. u32 tmp;
  891. if (is_gen2)
  892. hotplug_offset = PDC2_SATA_PLUG_CSR;
  893. else
  894. hotplug_offset = PDC_SATA_PLUG_CSR;
  895. /*
  896. * Except for the hotplug stuff, this is voodoo from the
  897. * Promise driver. Label this entire section
  898. * "TODO: figure out why we do this"
  899. */
  900. /* enable BMR_BURST, maybe change FIFO_SHD to 8 dwords */
  901. tmp = readl(mmio + PDC_FLASH_CTL);
  902. tmp |= 0x02000; /* bit 13 (enable bmr burst) */
  903. if (!is_gen2)
  904. tmp |= 0x10000; /* bit 16 (fifo threshold at 8 dw) */
  905. writel(tmp, mmio + PDC_FLASH_CTL);
  906. /* clear plug/unplug flags for all ports */
  907. tmp = readl(mmio + hotplug_offset);
  908. writel(tmp | 0xff, mmio + hotplug_offset);
  909. /* unmask plug/unplug ints */
  910. tmp = readl(mmio + hotplug_offset);
  911. writel(tmp & ~0xff0000, mmio + hotplug_offset);
  912. /* don't initialise TBG or SLEW on 2nd generation chips */
  913. if (is_gen2)
  914. return;
  915. /* reduce TBG clock to 133 Mhz. */
  916. tmp = readl(mmio + PDC_TBG_MODE);
  917. tmp &= ~0x30000; /* clear bit 17, 16*/
  918. tmp |= 0x10000; /* set bit 17:16 = 0:1 */
  919. writel(tmp, mmio + PDC_TBG_MODE);
  920. readl(mmio + PDC_TBG_MODE); /* flush */
  921. msleep(10);
  922. /* adjust slew rate control register. */
  923. tmp = readl(mmio + PDC_SLEW_CTL);
  924. tmp &= 0xFFFFF03F; /* clear bit 11 ~ 6 */
  925. tmp |= 0x00000900; /* set bit 11-9 = 100b , bit 8-6 = 100 */
  926. writel(tmp, mmio + PDC_SLEW_CTL);
  927. }
  928. static int pdc_ata_init_one(struct pci_dev *pdev,
  929. const struct pci_device_id *ent)
  930. {
  931. static int printed_version;
  932. const struct ata_port_info *pi = &pdc_port_info[ent->driver_data];
  933. const struct ata_port_info *ppi[PDC_MAX_PORTS];
  934. struct ata_host *host;
  935. void __iomem *base;
  936. int n_ports, i, rc;
  937. int is_sataii_tx4;
  938. if (!printed_version++)
  939. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  940. /* enable and acquire resources */
  941. rc = pcim_enable_device(pdev);
  942. if (rc)
  943. return rc;
  944. rc = pcim_iomap_regions(pdev, 1 << PDC_MMIO_BAR, DRV_NAME);
  945. if (rc == -EBUSY)
  946. pcim_pin_device(pdev);
  947. if (rc)
  948. return rc;
  949. base = pcim_iomap_table(pdev)[PDC_MMIO_BAR];
  950. /* determine port configuration and setup host */
  951. n_ports = 2;
  952. if (pi->flags & PDC_FLAG_4_PORTS)
  953. n_ports = 4;
  954. for (i = 0; i < n_ports; i++)
  955. ppi[i] = pi;
  956. if (pi->flags & PDC_FLAG_SATA_PATA) {
  957. u8 tmp = readb(base + PDC_FLASH_CTL+1);
  958. if (!(tmp & 0x80))
  959. ppi[n_ports++] = pi + 1;
  960. }
  961. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  962. if (!host) {
  963. dev_printk(KERN_ERR, &pdev->dev, "failed to allocate host\n");
  964. return -ENOMEM;
  965. }
  966. host->iomap = pcim_iomap_table(pdev);
  967. is_sataii_tx4 = pdc_is_sataii_tx4(pi->flags);
  968. for (i = 0; i < host->n_ports; i++) {
  969. struct ata_port *ap = host->ports[i];
  970. unsigned int ata_no = pdc_port_no_to_ata_no(i, is_sataii_tx4);
  971. unsigned int port_offset = 0x200 + ata_no * 0x80;
  972. unsigned int scr_offset = 0x400 + ata_no * 0x100;
  973. pdc_ata_setup_port(ap, base + port_offset, base + scr_offset);
  974. ata_port_pbar_desc(ap, PDC_MMIO_BAR, -1, "mmio");
  975. ata_port_pbar_desc(ap, PDC_MMIO_BAR, port_offset, "port");
  976. }
  977. /* initialize adapter */
  978. pdc_host_init(host);
  979. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  980. if (rc)
  981. return rc;
  982. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  983. if (rc)
  984. return rc;
  985. /* start host, request IRQ and attach */
  986. pci_set_master(pdev);
  987. return ata_host_activate(host, pdev->irq, pdc_interrupt, IRQF_SHARED,
  988. &pdc_ata_sht);
  989. }
  990. static int __init pdc_ata_init(void)
  991. {
  992. return pci_register_driver(&pdc_ata_pci_driver);
  993. }
  994. static void __exit pdc_ata_exit(void)
  995. {
  996. pci_unregister_driver(&pdc_ata_pci_driver);
  997. }
  998. MODULE_AUTHOR("Jeff Garzik");
  999. MODULE_DESCRIPTION("Promise ATA TX2/TX4/TX4000 low-level driver");
  1000. MODULE_LICENSE("GPL");
  1001. MODULE_DEVICE_TABLE(pci, pdc_ata_pci_tbl);
  1002. MODULE_VERSION(DRV_VERSION);
  1003. module_init(pdc_ata_init);
  1004. module_exit(pdc_ata_exit);