sata_sil24.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. * sata_sil24.c - Driver for Silicon Image 3124/3132 SATA-2 controllers
  3. *
  4. * Copyright 2005 Tejun Heo
  5. *
  6. * Based on preview driver from Silicon Image.
  7. *
  8. * NOTE: No NCQ/ATAPI support yet. The preview driver didn't support
  9. * NCQ nor ATAPI, and, unfortunately, I couldn't find out how to make
  10. * those work. Enabling those shouldn't be difficult. Basic
  11. * structure is all there (in libata-dev tree). If you have any
  12. * information about this hardware, please contact me or linux-ide.
  13. * Info is needed on...
  14. *
  15. * - How to issue tagged commands and turn on sactive on issue accordingly.
  16. * - Where to put an ATAPI command and how to tell the device to send it.
  17. * - How to enable/use 64bit.
  18. *
  19. * This program is free software; you can redistribute it and/or modify it
  20. * under the terms of the GNU General Public License as published by the
  21. * Free Software Foundation; either version 2, or (at your option) any
  22. * later version.
  23. *
  24. * This program is distributed in the hope that it will be useful, but
  25. * WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * General Public License for more details.
  28. *
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/pci.h>
  33. #include <linux/blkdev.h>
  34. #include <linux/delay.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/dma-mapping.h>
  37. #include <scsi/scsi_host.h>
  38. #include "scsi.h"
  39. #include <linux/libata.h>
  40. #include <asm/io.h>
  41. #define DRV_NAME "sata_sil24"
  42. #define DRV_VERSION "0.20" /* Silicon Image's preview driver was 0.10 */
  43. #define NR_PORTS 4
  44. /*
  45. * Port request block (PRB) 32 bytes
  46. */
  47. struct sil24_prb {
  48. u16 ctrl;
  49. u16 prot;
  50. u32 rx_cnt;
  51. u8 fis[6 * 4];
  52. };
  53. /*
  54. * Scatter gather entry (SGE) 16 bytes
  55. */
  56. struct sil24_sge {
  57. u64 addr;
  58. u32 cnt;
  59. u32 flags;
  60. };
  61. /*
  62. * Port multiplier
  63. */
  64. struct sil24_port_multiplier {
  65. u32 diag;
  66. u32 sactive;
  67. };
  68. enum {
  69. /*
  70. * Global controller registers (128 bytes @ BAR0)
  71. */
  72. /* 32 bit regs */
  73. HOST_SLOT_STAT = 0x00, /* 32 bit slot stat * 4 */
  74. HOST_CTRL = 0x40,
  75. HOST_IRQ_STAT = 0x44,
  76. HOST_PHY_CFG = 0x48,
  77. HOST_BIST_CTRL = 0x50,
  78. HOST_BIST_PTRN = 0x54,
  79. HOST_BIST_STAT = 0x58,
  80. HOST_MEM_BIST_STAT = 0x5c,
  81. HOST_FLASH_CMD = 0x70,
  82. /* 8 bit regs */
  83. HOST_FLASH_DATA = 0x74,
  84. HOST_TRANSITION_DETECT = 0x75,
  85. HOST_GPIO_CTRL = 0x76,
  86. HOST_I2C_ADDR = 0x78, /* 32 bit */
  87. HOST_I2C_DATA = 0x7c,
  88. HOST_I2C_XFER_CNT = 0x7e,
  89. HOST_I2C_CTRL = 0x7f,
  90. /* HOST_SLOT_STAT bits */
  91. HOST_SSTAT_ATTN = (1 << 31),
  92. /*
  93. * Port registers
  94. * (8192 bytes @ +0x0000, +0x2000, +0x4000 and +0x6000 @ BAR2)
  95. */
  96. PORT_REGS_SIZE = 0x2000,
  97. PORT_PRB = 0x0000, /* (32 bytes PRB + 16 bytes SGEs * 6) * 31 (3968 bytes) */
  98. /* TF is overlayed w/ PRB regs in the preview driver,
  99. * but it doesn't seem to work. */
  100. PORT_TF = 0x0000,
  101. PORT_PM = 0x0f80, /* 8 bytes PM * 16 (128 bytes) */
  102. /* 32 bit regs */
  103. PORT_CTRL_STAT = 0x1000, /* write:ctrl, read:stat */
  104. PORT_CTRL_CLR = 0x1004,
  105. PORT_IRQ_STAT = 0x1008,
  106. PORT_IRQ_ENABLE_SET = 0x1010,
  107. PORT_IRQ_ENABLE_CLR = 0x1014,
  108. PORT_ACTIVATE_UPPER_ADDR= 0x101c,
  109. PORT_EXEC_FIFO = 0x1020,
  110. PORT_CMD_ERR = 0x1024,
  111. PORT_FIS_CFG = 0x1028,
  112. PORT_FIFO_THRES = 0x102c,
  113. /* 16 bit regs */
  114. PORT_DECODE_ERR_CNT = 0x1040,
  115. PORT_DECODE_ERR_THRESH = 0x1042,
  116. PORT_CRC_ERR_CNT = 0x1044,
  117. PORT_CRC_ERR_THRESH = 0x1046,
  118. PORT_HSHK_ERR_CNT = 0x1048,
  119. PORT_HSHK_ERR_THRESH = 0x104a,
  120. /* 32 bit regs */
  121. PORT_PHY_CFG = 0x1050,
  122. PORT_SLOT_STAT = 0x1800,
  123. PORT_CMD_ACTIVATE = 0x1c00, /* 64 bit cmd activate * 31 (248 bytes) */
  124. PORT_EXEC_DIAG = 0x1e00, /* 32bit exec diag * 16 (64 bytes, 0-10 used on 3124) */
  125. PORT_PSD_DIAG = 0x1e40, /* 32bit psd diag * 16 (64 bytes, 0-8 used on 3124) */
  126. PORT_SCONTROL = 0x1f00,
  127. PORT_SSTATUS = 0x1f04,
  128. PORT_SERROR = 0x1f08,
  129. PORT_SACTIVE = 0x1f0c,
  130. /* PORT_CTRL_STAT bits */
  131. PORT_CS_PORT_RST = (1 << 0), /* port reset */
  132. PORT_CS_DEV_RST = (1 << 1), /* device reset */
  133. PORT_CS_INIT = (1 << 2), /* port initialize */
  134. PORT_CS_IRQ_WOC = (1 << 3), /* interrupt write one to clear */
  135. PORT_CS_RESUME = (1 << 4), /* port resume */
  136. PORT_CS_32BIT_ACTV = (1 << 5), /* 32-bit activation */
  137. PORT_CS_PM_EN = (1 << 6), /* port multiplier enable */
  138. PORT_CS_RDY = (1 << 7), /* port ready to accept commands */
  139. /* PORT_IRQ_STAT/ENABLE_SET/CLR */
  140. /* bits[11:0] are masked */
  141. PORT_IRQ_COMPLETE = (1 << 0), /* command(s) completed */
  142. PORT_IRQ_ERROR = (1 << 1), /* command execution error */
  143. PORT_IRQ_PORTRDY_CHG = (1 << 2), /* port ready change */
  144. PORT_IRQ_PWR_CHG = (1 << 3), /* power management change */
  145. PORT_IRQ_PHYRDY_CHG = (1 << 4), /* PHY ready change */
  146. PORT_IRQ_COMWAKE = (1 << 5), /* COMWAKE received */
  147. PORT_IRQ_UNK_FIS = (1 << 6), /* Unknown FIS received */
  148. PORT_IRQ_SDB_FIS = (1 << 11), /* SDB FIS received */
  149. /* bits[27:16] are unmasked (raw) */
  150. PORT_IRQ_RAW_SHIFT = 16,
  151. PORT_IRQ_MASKED_MASK = 0x7ff,
  152. PORT_IRQ_RAW_MASK = (0x7ff << PORT_IRQ_RAW_SHIFT),
  153. /* ENABLE_SET/CLR specific, intr steering - 2 bit field */
  154. PORT_IRQ_STEER_SHIFT = 30,
  155. PORT_IRQ_STEER_MASK = (3 << PORT_IRQ_STEER_SHIFT),
  156. /* PORT_CMD_ERR constants */
  157. PORT_CERR_DEV = 1, /* Error bit in D2H Register FIS */
  158. PORT_CERR_SDB = 2, /* Error bit in SDB FIS */
  159. PORT_CERR_DATA = 3, /* Error in data FIS not detected by dev */
  160. PORT_CERR_SEND = 4, /* Initial cmd FIS transmission failure */
  161. PORT_CERR_INCONSISTENT = 5, /* Protocol mismatch */
  162. PORT_CERR_DIRECTION = 6, /* Data direction mismatch */
  163. PORT_CERR_UNDERRUN = 7, /* Ran out of SGEs while writing */
  164. PORT_CERR_OVERRUN = 8, /* Ran out of SGEs while reading */
  165. PORT_CERR_PKT_PROT = 11, /* DIR invalid in 1st PIO setup of ATAPI */
  166. PORT_CERR_SGT_BOUNDARY = 16, /* PLD ecode 00 - SGT not on qword boundary */
  167. PORT_CERR_SGT_TGTABRT = 17, /* PLD ecode 01 - target abort */
  168. PORT_CERR_SGT_MSTABRT = 18, /* PLD ecode 10 - master abort */
  169. PORT_CERR_SGT_PCIPERR = 19, /* PLD ecode 11 - PCI parity err while fetching SGT */
  170. PORT_CERR_CMD_BOUNDARY = 24, /* ctrl[15:13] 001 - PRB not on qword boundary */
  171. PORT_CERR_CMD_TGTABRT = 25, /* ctrl[15:13] 010 - target abort */
  172. PORT_CERR_CMD_MSTABRT = 26, /* ctrl[15:13] 100 - master abort */
  173. PORT_CERR_CMD_PCIPERR = 27, /* ctrl[15:13] 110 - PCI parity err while fetching PRB */
  174. PORT_CERR_XFR_UNDEF = 32, /* PSD ecode 00 - undefined */
  175. PORT_CERR_XFR_TGTABRT = 33, /* PSD ecode 01 - target abort */
  176. PORT_CERR_XFR_MSGABRT = 34, /* PSD ecode 10 - master abort */
  177. PORT_CERR_XFR_PCIPERR = 35, /* PSD ecode 11 - PCI prity err during transfer */
  178. PORT_CERR_SENDSERVICE = 36, /* FIS received whiel sending service */
  179. /*
  180. * Other constants
  181. */
  182. SGE_TRM = (1 << 31), /* Last SGE in chain */
  183. PRB_SOFT_RST = (1 << 7), /* Soft reset request (ign BSY?) */
  184. /* board id */
  185. BID_SIL3124 = 0,
  186. BID_SIL3132 = 1,
  187. IRQ_STAT_4PORTS = 0xf,
  188. };
  189. struct sil24_cmd_block {
  190. struct sil24_prb prb;
  191. struct sil24_sge sge[LIBATA_MAX_PRD];
  192. };
  193. /*
  194. * ap->private_data
  195. *
  196. * The preview driver always returned 0 for status. We emulate it
  197. * here from the previous interrupt.
  198. */
  199. struct sil24_port_priv {
  200. void *port;
  201. struct sil24_cmd_block *cmd_block; /* 32 cmd blocks */
  202. dma_addr_t cmd_block_dma; /* DMA base addr for them */
  203. };
  204. /* ap->host_set->private_data */
  205. struct sil24_host_priv {
  206. void *host_base; /* global controller control (128 bytes @BAR0) */
  207. void *port_base; /* port registers (4 * 8192 bytes @BAR2) */
  208. };
  209. static u8 sil24_check_status(struct ata_port *ap);
  210. static u8 sil24_check_err(struct ata_port *ap);
  211. static u32 sil24_scr_read(struct ata_port *ap, unsigned sc_reg);
  212. static void sil24_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val);
  213. static void sil24_phy_reset(struct ata_port *ap);
  214. static void sil24_qc_prep(struct ata_queued_cmd *qc);
  215. static int sil24_qc_issue(struct ata_queued_cmd *qc);
  216. static void sil24_irq_clear(struct ata_port *ap);
  217. static void sil24_eng_timeout(struct ata_port *ap);
  218. static irqreturn_t sil24_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
  219. static int sil24_port_start(struct ata_port *ap);
  220. static void sil24_port_stop(struct ata_port *ap);
  221. static void sil24_host_stop(struct ata_host_set *host_set);
  222. static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  223. static struct pci_device_id sil24_pci_tbl[] = {
  224. { 0x1095, 0x3124, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3124 },
  225. { 0x1095, 0x3132, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3132 },
  226. };
  227. static struct pci_driver sil24_pci_driver = {
  228. .name = DRV_NAME,
  229. .id_table = sil24_pci_tbl,
  230. .probe = sil24_init_one,
  231. .remove = ata_pci_remove_one, /* safe? */
  232. };
  233. static Scsi_Host_Template sil24_sht = {
  234. .module = THIS_MODULE,
  235. .name = DRV_NAME,
  236. .ioctl = ata_scsi_ioctl,
  237. .queuecommand = ata_scsi_queuecmd,
  238. .eh_strategy_handler = ata_scsi_error,
  239. .can_queue = ATA_DEF_QUEUE,
  240. .this_id = ATA_SHT_THIS_ID,
  241. .sg_tablesize = LIBATA_MAX_PRD,
  242. .max_sectors = ATA_MAX_SECTORS,
  243. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  244. .emulated = ATA_SHT_EMULATED,
  245. .use_clustering = ATA_SHT_USE_CLUSTERING,
  246. .proc_name = DRV_NAME,
  247. .dma_boundary = ATA_DMA_BOUNDARY,
  248. .slave_configure = ata_scsi_slave_config,
  249. .bios_param = ata_std_bios_param,
  250. .ordered_flush = 1, /* NCQ not supported yet */
  251. };
  252. static struct ata_port_operations sil24_ops = {
  253. .port_disable = ata_port_disable,
  254. .check_status = sil24_check_status,
  255. .check_altstatus = sil24_check_status,
  256. .check_err = sil24_check_err,
  257. .dev_select = ata_noop_dev_select,
  258. .phy_reset = sil24_phy_reset,
  259. .qc_prep = sil24_qc_prep,
  260. .qc_issue = sil24_qc_issue,
  261. .eng_timeout = sil24_eng_timeout,
  262. .irq_handler = sil24_interrupt,
  263. .irq_clear = sil24_irq_clear,
  264. .scr_read = sil24_scr_read,
  265. .scr_write = sil24_scr_write,
  266. .port_start = sil24_port_start,
  267. .port_stop = sil24_port_stop,
  268. .host_stop = sil24_host_stop,
  269. };
  270. static struct ata_port_info sil24_port_info[] = {
  271. /* sil_3124 */
  272. {
  273. .sht = &sil24_sht,
  274. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  275. ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
  276. ATA_FLAG_PIO_DMA,
  277. .pio_mask = 0x1f, /* pio0-4 */
  278. .mwdma_mask = 0x07, /* mwdma0-2 */
  279. .udma_mask = 0x3f, /* udma0-5 */
  280. .port_ops = &sil24_ops,
  281. },
  282. /* sil_3132 */
  283. {
  284. .sht = &sil24_sht,
  285. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  286. ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
  287. ATA_FLAG_PIO_DMA,
  288. .pio_mask = 0x1f, /* pio0-4 */
  289. .mwdma_mask = 0x07, /* mwdma0-2 */
  290. .udma_mask = 0x3f, /* udma0-5 */
  291. .port_ops = &sil24_ops,
  292. },
  293. };
  294. static u8 sil24_check_status(struct ata_port *ap)
  295. {
  296. return ATA_DRDY;
  297. }
  298. static u8 sil24_check_err(struct ata_port *ap)
  299. {
  300. return 0;
  301. }
  302. static int sil24_scr_map[] = {
  303. [SCR_CONTROL] = 0,
  304. [SCR_STATUS] = 1,
  305. [SCR_ERROR] = 2,
  306. [SCR_ACTIVE] = 3,
  307. };
  308. static u32 sil24_scr_read(struct ata_port *ap, unsigned sc_reg)
  309. {
  310. void *scr_addr = (void *)ap->ioaddr.scr_addr;
  311. if (sc_reg < ARRAY_SIZE(sil24_scr_map)) {
  312. void *addr;
  313. addr = scr_addr + sil24_scr_map[sc_reg] * 4;
  314. return readl(scr_addr + sil24_scr_map[sc_reg] * 4);
  315. }
  316. return 0xffffffffU;
  317. }
  318. static void sil24_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val)
  319. {
  320. void *scr_addr = (void *)ap->ioaddr.scr_addr;
  321. if (sc_reg < ARRAY_SIZE(sil24_scr_map)) {
  322. void *addr;
  323. addr = scr_addr + sil24_scr_map[sc_reg] * 4;
  324. writel(val, scr_addr + sil24_scr_map[sc_reg] * 4);
  325. }
  326. }
  327. static void sil24_phy_reset(struct ata_port *ap)
  328. {
  329. __sata_phy_reset(ap);
  330. /*
  331. * No ATAPI yet. Just unconditionally indicate ATA device.
  332. * If ATAPI device is attached, it will fail ATA_CMD_ID_ATA
  333. * and libata core will ignore the device.
  334. */
  335. if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
  336. ap->device[0].class = ATA_DEV_ATA;
  337. }
  338. static inline void sil24_fill_sg(struct ata_queued_cmd *qc,
  339. struct sil24_cmd_block *cb)
  340. {
  341. struct scatterlist *sg = qc->sg;
  342. struct sil24_sge *sge = cb->sge;
  343. unsigned i;
  344. for (i = 0; i < qc->n_elem; i++, sg++, sge++) {
  345. sge->addr = cpu_to_le64(sg_dma_address(sg));
  346. sge->cnt = cpu_to_le32(sg_dma_len(sg));
  347. sge->flags = 0;
  348. sge->flags = i < qc->n_elem - 1 ? 0 : cpu_to_le32(SGE_TRM);
  349. }
  350. }
  351. static void sil24_qc_prep(struct ata_queued_cmd *qc)
  352. {
  353. struct ata_port *ap = qc->ap;
  354. struct sil24_port_priv *pp = ap->private_data;
  355. struct sil24_cmd_block *cb = pp->cmd_block + qc->tag;
  356. struct sil24_prb *prb = &cb->prb;
  357. switch (qc->tf.protocol) {
  358. case ATA_PROT_PIO:
  359. case ATA_PROT_DMA:
  360. case ATA_PROT_NODATA:
  361. break;
  362. default:
  363. /* ATAPI isn't supported yet */
  364. BUG();
  365. }
  366. ata_tf_to_fis(&qc->tf, prb->fis, 0);
  367. if (qc->flags & ATA_QCFLAG_DMAMAP)
  368. sil24_fill_sg(qc, cb);
  369. }
  370. static int sil24_qc_issue(struct ata_queued_cmd *qc)
  371. {
  372. struct ata_port *ap = qc->ap;
  373. struct sil24_port_priv *pp = ap->private_data;
  374. dma_addr_t paddr = pp->cmd_block_dma + qc->tag * sizeof(*pp->cmd_block);
  375. writel((u32)paddr, pp->port + PORT_CMD_ACTIVATE);
  376. return 0;
  377. }
  378. static void sil24_irq_clear(struct ata_port *ap)
  379. {
  380. /* unused */
  381. }
  382. static void sil24_reset_controller(struct ata_port *ap)
  383. {
  384. struct sil24_port_priv *pp = ap->private_data;
  385. void *port = pp->port;
  386. int cnt;
  387. u32 tmp;
  388. printk(KERN_NOTICE DRV_NAME
  389. " ata%u: resetting controller...\n", ap->id);
  390. /* Reset controller state. Is this correct? */
  391. writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT);
  392. readl(port + PORT_CTRL_STAT); /* sync */
  393. /* Max ~100ms */
  394. for (cnt = 0; cnt < 1000; cnt++) {
  395. udelay(100);
  396. tmp = readl(port + PORT_CTRL_STAT);
  397. if (!(tmp & PORT_CS_DEV_RST))
  398. break;
  399. }
  400. if (tmp & PORT_CS_DEV_RST)
  401. printk(KERN_ERR DRV_NAME
  402. " ata%u: failed to reset controller\n", ap->id);
  403. }
  404. static void sil24_eng_timeout(struct ata_port *ap)
  405. {
  406. struct ata_queued_cmd *qc;
  407. qc = ata_qc_from_tag(ap, ap->active_tag);
  408. if (!qc) {
  409. printk(KERN_ERR "ata%u: BUG: tiemout without command\n",
  410. ap->id);
  411. return;
  412. }
  413. /*
  414. * hack alert! We cannot use the supplied completion
  415. * function from inside the ->eh_strategy_handler() thread.
  416. * libata is the only user of ->eh_strategy_handler() in
  417. * any kernel, so the default scsi_done() assumes it is
  418. * not being called from the SCSI EH.
  419. */
  420. printk(KERN_ERR "ata%u: command timeout\n", ap->id);
  421. qc->scsidone = scsi_finish_command;
  422. ata_qc_complete(qc, ATA_ERR);
  423. sil24_reset_controller(ap);
  424. }
  425. static inline void sil24_host_intr(struct ata_port *ap)
  426. {
  427. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->active_tag);
  428. struct sil24_port_priv *pp = ap->private_data;
  429. void *port = pp->port;
  430. u32 slot_stat;
  431. slot_stat = readl(port + PORT_SLOT_STAT);
  432. if (!(slot_stat & HOST_SSTAT_ATTN)) {
  433. if (qc)
  434. ata_qc_complete(qc, 0);
  435. } else {
  436. u32 irq_stat, cmd_err, sstatus, serror;
  437. irq_stat = readl(port + PORT_IRQ_STAT);
  438. cmd_err = readl(port + PORT_CMD_ERR);
  439. sstatus = readl(port + PORT_SSTATUS);
  440. serror = readl(port + PORT_SERROR);
  441. /* Clear IRQ/errors */
  442. writel(irq_stat, port + PORT_IRQ_STAT);
  443. if (cmd_err)
  444. writel(cmd_err, port + PORT_CMD_ERR);
  445. if (serror)
  446. writel(serror, port + PORT_SERROR);
  447. printk(KERN_ERR DRV_NAME " ata%u: error interrupt on port%d\n"
  448. " stat=0x%x irq=0x%x cmd_err=%d sstatus=0x%x serror=0x%x\n",
  449. ap->id, ap->port_no, slot_stat, irq_stat, cmd_err, sstatus, serror);
  450. if (qc)
  451. ata_qc_complete(qc, ATA_ERR);
  452. sil24_reset_controller(ap);
  453. }
  454. }
  455. static irqreturn_t sil24_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
  456. {
  457. struct ata_host_set *host_set = dev_instance;
  458. struct sil24_host_priv *hpriv = host_set->private_data;
  459. unsigned handled = 0;
  460. u32 status;
  461. int i;
  462. status = readl(hpriv->host_base + HOST_IRQ_STAT);
  463. if (status == 0xffffffff) {
  464. printk(KERN_ERR DRV_NAME ": IRQ status == 0xffffffff, "
  465. "PCI fault or device removal?\n");
  466. goto out;
  467. }
  468. if (!(status & IRQ_STAT_4PORTS))
  469. goto out;
  470. spin_lock(&host_set->lock);
  471. for (i = 0; i < host_set->n_ports; i++)
  472. if (status & (1 << i)) {
  473. struct ata_port *ap = host_set->ports[i];
  474. if (ap && !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
  475. sil24_host_intr(host_set->ports[i]);
  476. handled++;
  477. } else
  478. printk(KERN_ERR DRV_NAME
  479. ": interrupt from disabled port %d\n", i);
  480. }
  481. spin_unlock(&host_set->lock);
  482. out:
  483. return IRQ_RETVAL(handled);
  484. }
  485. static int sil24_port_start(struct ata_port *ap)
  486. {
  487. struct device *dev = ap->host_set->dev;
  488. struct sil24_host_priv *hpriv = ap->host_set->private_data;
  489. struct sil24_port_priv *pp;
  490. struct sil24_cmd_block *cb;
  491. size_t cb_size = sizeof(*cb);
  492. dma_addr_t cb_dma;
  493. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  494. if (!pp)
  495. return -ENOMEM;
  496. memset(pp, 0, sizeof(*pp));
  497. cb = dma_alloc_coherent(dev, cb_size, &cb_dma, GFP_KERNEL);
  498. if (!cb) {
  499. kfree(pp);
  500. return -ENOMEM;
  501. }
  502. memset(cb, 0, cb_size);
  503. pp->port = hpriv->port_base + ap->port_no * PORT_REGS_SIZE;
  504. pp->cmd_block = cb;
  505. pp->cmd_block_dma = cb_dma;
  506. ap->private_data = pp;
  507. return 0;
  508. }
  509. static void sil24_port_stop(struct ata_port *ap)
  510. {
  511. struct device *dev = ap->host_set->dev;
  512. struct sil24_port_priv *pp = ap->private_data;
  513. size_t cb_size = sizeof(*pp->cmd_block);
  514. dma_free_coherent(dev, cb_size, pp->cmd_block, pp->cmd_block_dma);
  515. kfree(pp);
  516. }
  517. static void sil24_host_stop(struct ata_host_set *host_set)
  518. {
  519. struct sil24_host_priv *hpriv = host_set->private_data;
  520. iounmap(hpriv->host_base);
  521. iounmap(hpriv->port_base);
  522. kfree(hpriv);
  523. }
  524. static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  525. {
  526. static int printed_version = 0;
  527. unsigned int board_id = (unsigned int)ent->driver_data;
  528. struct ata_probe_ent *probe_ent = NULL;
  529. struct sil24_host_priv *hpriv = NULL;
  530. void *host_base = NULL, *port_base = NULL;
  531. int i, rc;
  532. if (!printed_version++)
  533. printk(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
  534. rc = pci_enable_device(pdev);
  535. if (rc)
  536. return rc;
  537. rc = pci_request_regions(pdev, DRV_NAME);
  538. if (rc)
  539. goto out_disable;
  540. rc = -ENOMEM;
  541. /* ioremap mmio registers */
  542. host_base = ioremap(pci_resource_start(pdev, 0),
  543. pci_resource_len(pdev, 0));
  544. if (!host_base)
  545. goto out_free;
  546. port_base = ioremap(pci_resource_start(pdev, 2),
  547. pci_resource_len(pdev, 2));
  548. if (!port_base)
  549. goto out_free;
  550. /* allocate & init probe_ent and hpriv */
  551. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  552. if (!probe_ent)
  553. goto out_free;
  554. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  555. if (!hpriv)
  556. goto out_free;
  557. memset(probe_ent, 0, sizeof(*probe_ent));
  558. probe_ent->dev = pci_dev_to_dev(pdev);
  559. INIT_LIST_HEAD(&probe_ent->node);
  560. probe_ent->sht = sil24_port_info[board_id].sht;
  561. probe_ent->host_flags = sil24_port_info[board_id].host_flags;
  562. probe_ent->pio_mask = sil24_port_info[board_id].pio_mask;
  563. probe_ent->udma_mask = sil24_port_info[board_id].udma_mask;
  564. probe_ent->port_ops = sil24_port_info[board_id].port_ops;
  565. probe_ent->n_ports = (board_id == BID_SIL3124) ? 4 : 2;
  566. probe_ent->irq = pdev->irq;
  567. probe_ent->irq_flags = SA_SHIRQ;
  568. probe_ent->mmio_base = port_base;
  569. probe_ent->private_data = hpriv;
  570. memset(hpriv, 0, sizeof(*hpriv));
  571. hpriv->host_base = host_base;
  572. hpriv->port_base = port_base;
  573. /*
  574. * Configure the device
  575. */
  576. /*
  577. * FIXME: This device is certainly 64-bit capable. We just
  578. * don't know how to use it. After fixing 32bit activation in
  579. * this function, enable 64bit masks here.
  580. */
  581. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  582. if (rc) {
  583. printk(KERN_ERR DRV_NAME "(%s): 32-bit DMA enable failed\n",
  584. pci_name(pdev));
  585. goto out_free;
  586. }
  587. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  588. if (rc) {
  589. printk(KERN_ERR DRV_NAME "(%s): 32-bit consistent DMA enable failed\n",
  590. pci_name(pdev));
  591. goto out_free;
  592. }
  593. /* GPIO off */
  594. writel(0, host_base + HOST_FLASH_CMD);
  595. /* Mask interrupts during initialization */
  596. writel(0, host_base + HOST_CTRL);
  597. for (i = 0; i < probe_ent->n_ports; i++) {
  598. void *port = port_base + i * PORT_REGS_SIZE;
  599. unsigned long portu = (unsigned long)port;
  600. u32 tmp;
  601. int cnt;
  602. probe_ent->port[i].cmd_addr = portu + PORT_TF;
  603. probe_ent->port[i].ctl_addr = portu + PORT_TF + 0xa;
  604. probe_ent->port[i].altstatus_addr = portu + PORT_TF + 0xa;
  605. probe_ent->port[i].scr_addr = portu + PORT_SCONTROL;
  606. ata_std_ports(&probe_ent->port[i]);
  607. /* Initial PHY setting */
  608. writel(0x20c, port + PORT_PHY_CFG);
  609. /* Clear port RST */
  610. tmp = readl(port + PORT_CTRL_STAT);
  611. if (tmp & PORT_CS_PORT_RST) {
  612. writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
  613. readl(port + PORT_CTRL_STAT); /* sync */
  614. for (cnt = 0; cnt < 10; cnt++) {
  615. msleep(10);
  616. tmp = readl(port + PORT_CTRL_STAT);
  617. if (!(tmp & PORT_CS_PORT_RST))
  618. break;
  619. }
  620. if (tmp & PORT_CS_PORT_RST)
  621. printk(KERN_ERR DRV_NAME
  622. "(%s): failed to clear port RST\n",
  623. pci_name(pdev));
  624. }
  625. /* Zero error counters. */
  626. writel(0x8000, port + PORT_DECODE_ERR_THRESH);
  627. writel(0x8000, port + PORT_CRC_ERR_THRESH);
  628. writel(0x8000, port + PORT_HSHK_ERR_THRESH);
  629. writel(0x0000, port + PORT_DECODE_ERR_CNT);
  630. writel(0x0000, port + PORT_CRC_ERR_CNT);
  631. writel(0x0000, port + PORT_HSHK_ERR_CNT);
  632. /* FIXME: 32bit activation? */
  633. writel(0, port + PORT_ACTIVATE_UPPER_ADDR);
  634. writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_STAT);
  635. /* Configure interrupts */
  636. writel(0xffff, port + PORT_IRQ_ENABLE_CLR);
  637. writel(PORT_IRQ_COMPLETE | PORT_IRQ_ERROR | PORT_IRQ_SDB_FIS,
  638. port + PORT_IRQ_ENABLE_SET);
  639. /* Clear interrupts */
  640. writel(0x0fff0fff, port + PORT_IRQ_STAT);
  641. writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR);
  642. }
  643. /* Turn on interrupts */
  644. writel(IRQ_STAT_4PORTS, host_base + HOST_CTRL);
  645. pci_set_master(pdev);
  646. /* FIXME: check ata_device_add return value */
  647. ata_device_add(probe_ent);
  648. kfree(probe_ent);
  649. return 0;
  650. out_free:
  651. if (host_base)
  652. iounmap(host_base);
  653. if (port_base)
  654. iounmap(port_base);
  655. kfree(probe_ent);
  656. kfree(hpriv);
  657. pci_release_regions(pdev);
  658. out_disable:
  659. pci_disable_device(pdev);
  660. return rc;
  661. }
  662. static int __init sil24_init(void)
  663. {
  664. return pci_module_init(&sil24_pci_driver);
  665. }
  666. static void __exit sil24_exit(void)
  667. {
  668. pci_unregister_driver(&sil24_pci_driver);
  669. }
  670. MODULE_AUTHOR("Tejun Heo");
  671. MODULE_DESCRIPTION("Silicon Image 3124/3132 SATA low-level driver");
  672. MODULE_LICENSE("GPL");
  673. MODULE_DEVICE_TABLE(pci, sil24_pci_tbl);
  674. module_init(sil24_init);
  675. module_exit(sil24_exit);