sata_sil24.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2, or (at your option) any
  11. * later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/pci.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/delay.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/device.h>
  27. #include <scsi/scsi_host.h>
  28. #include <scsi/scsi_cmnd.h>
  29. #include <linux/libata.h>
  30. #include <asm/io.h>
  31. #define DRV_NAME "sata_sil24"
  32. #define DRV_VERSION "0.23"
  33. /*
  34. * Port request block (PRB) 32 bytes
  35. */
  36. struct sil24_prb {
  37. u16 ctrl;
  38. u16 prot;
  39. u32 rx_cnt;
  40. u8 fis[6 * 4];
  41. };
  42. /*
  43. * Scatter gather entry (SGE) 16 bytes
  44. */
  45. struct sil24_sge {
  46. u64 addr;
  47. u32 cnt;
  48. u32 flags;
  49. };
  50. /*
  51. * Port multiplier
  52. */
  53. struct sil24_port_multiplier {
  54. u32 diag;
  55. u32 sactive;
  56. };
  57. enum {
  58. /*
  59. * Global controller registers (128 bytes @ BAR0)
  60. */
  61. /* 32 bit regs */
  62. HOST_SLOT_STAT = 0x00, /* 32 bit slot stat * 4 */
  63. HOST_CTRL = 0x40,
  64. HOST_IRQ_STAT = 0x44,
  65. HOST_PHY_CFG = 0x48,
  66. HOST_BIST_CTRL = 0x50,
  67. HOST_BIST_PTRN = 0x54,
  68. HOST_BIST_STAT = 0x58,
  69. HOST_MEM_BIST_STAT = 0x5c,
  70. HOST_FLASH_CMD = 0x70,
  71. /* 8 bit regs */
  72. HOST_FLASH_DATA = 0x74,
  73. HOST_TRANSITION_DETECT = 0x75,
  74. HOST_GPIO_CTRL = 0x76,
  75. HOST_I2C_ADDR = 0x78, /* 32 bit */
  76. HOST_I2C_DATA = 0x7c,
  77. HOST_I2C_XFER_CNT = 0x7e,
  78. HOST_I2C_CTRL = 0x7f,
  79. /* HOST_SLOT_STAT bits */
  80. HOST_SSTAT_ATTN = (1 << 31),
  81. /*
  82. * Port registers
  83. * (8192 bytes @ +0x0000, +0x2000, +0x4000 and +0x6000 @ BAR2)
  84. */
  85. PORT_REGS_SIZE = 0x2000,
  86. PORT_PRB = 0x0000, /* (32 bytes PRB + 16 bytes SGEs * 6) * 31 (3968 bytes) */
  87. PORT_PM = 0x0f80, /* 8 bytes PM * 16 (128 bytes) */
  88. /* 32 bit regs */
  89. PORT_CTRL_STAT = 0x1000, /* write: ctrl-set, read: stat */
  90. PORT_CTRL_CLR = 0x1004, /* write: ctrl-clear */
  91. PORT_IRQ_STAT = 0x1008, /* high: status, low: interrupt */
  92. PORT_IRQ_ENABLE_SET = 0x1010, /* write: enable-set */
  93. PORT_IRQ_ENABLE_CLR = 0x1014, /* write: enable-clear */
  94. PORT_ACTIVATE_UPPER_ADDR= 0x101c,
  95. PORT_EXEC_FIFO = 0x1020, /* command execution fifo */
  96. PORT_CMD_ERR = 0x1024, /* command error number */
  97. PORT_FIS_CFG = 0x1028,
  98. PORT_FIFO_THRES = 0x102c,
  99. /* 16 bit regs */
  100. PORT_DECODE_ERR_CNT = 0x1040,
  101. PORT_DECODE_ERR_THRESH = 0x1042,
  102. PORT_CRC_ERR_CNT = 0x1044,
  103. PORT_CRC_ERR_THRESH = 0x1046,
  104. PORT_HSHK_ERR_CNT = 0x1048,
  105. PORT_HSHK_ERR_THRESH = 0x104a,
  106. /* 32 bit regs */
  107. PORT_PHY_CFG = 0x1050,
  108. PORT_SLOT_STAT = 0x1800,
  109. PORT_CMD_ACTIVATE = 0x1c00, /* 64 bit cmd activate * 31 (248 bytes) */
  110. PORT_EXEC_DIAG = 0x1e00, /* 32bit exec diag * 16 (64 bytes, 0-10 used on 3124) */
  111. PORT_PSD_DIAG = 0x1e40, /* 32bit psd diag * 16 (64 bytes, 0-8 used on 3124) */
  112. PORT_SCONTROL = 0x1f00,
  113. PORT_SSTATUS = 0x1f04,
  114. PORT_SERROR = 0x1f08,
  115. PORT_SACTIVE = 0x1f0c,
  116. /* PORT_CTRL_STAT bits */
  117. PORT_CS_PORT_RST = (1 << 0), /* port reset */
  118. PORT_CS_DEV_RST = (1 << 1), /* device reset */
  119. PORT_CS_INIT = (1 << 2), /* port initialize */
  120. PORT_CS_IRQ_WOC = (1 << 3), /* interrupt write one to clear */
  121. PORT_CS_CDB16 = (1 << 5), /* 0=12b cdb, 1=16b cdb */
  122. PORT_CS_RESUME = (1 << 6), /* port resume */
  123. PORT_CS_32BIT_ACTV = (1 << 10), /* 32-bit activation */
  124. PORT_CS_PM_EN = (1 << 13), /* port multiplier enable */
  125. PORT_CS_RDY = (1 << 31), /* port ready to accept commands */
  126. /* PORT_IRQ_STAT/ENABLE_SET/CLR */
  127. /* bits[11:0] are masked */
  128. PORT_IRQ_COMPLETE = (1 << 0), /* command(s) completed */
  129. PORT_IRQ_ERROR = (1 << 1), /* command execution error */
  130. PORT_IRQ_PORTRDY_CHG = (1 << 2), /* port ready change */
  131. PORT_IRQ_PWR_CHG = (1 << 3), /* power management change */
  132. PORT_IRQ_PHYRDY_CHG = (1 << 4), /* PHY ready change */
  133. PORT_IRQ_COMWAKE = (1 << 5), /* COMWAKE received */
  134. PORT_IRQ_UNK_FIS = (1 << 6), /* Unknown FIS received */
  135. PORT_IRQ_SDB_FIS = (1 << 11), /* SDB FIS received */
  136. /* bits[27:16] are unmasked (raw) */
  137. PORT_IRQ_RAW_SHIFT = 16,
  138. PORT_IRQ_MASKED_MASK = 0x7ff,
  139. PORT_IRQ_RAW_MASK = (0x7ff << PORT_IRQ_RAW_SHIFT),
  140. /* ENABLE_SET/CLR specific, intr steering - 2 bit field */
  141. PORT_IRQ_STEER_SHIFT = 30,
  142. PORT_IRQ_STEER_MASK = (3 << PORT_IRQ_STEER_SHIFT),
  143. /* PORT_CMD_ERR constants */
  144. PORT_CERR_DEV = 1, /* Error bit in D2H Register FIS */
  145. PORT_CERR_SDB = 2, /* Error bit in SDB FIS */
  146. PORT_CERR_DATA = 3, /* Error in data FIS not detected by dev */
  147. PORT_CERR_SEND = 4, /* Initial cmd FIS transmission failure */
  148. PORT_CERR_INCONSISTENT = 5, /* Protocol mismatch */
  149. PORT_CERR_DIRECTION = 6, /* Data direction mismatch */
  150. PORT_CERR_UNDERRUN = 7, /* Ran out of SGEs while writing */
  151. PORT_CERR_OVERRUN = 8, /* Ran out of SGEs while reading */
  152. PORT_CERR_PKT_PROT = 11, /* DIR invalid in 1st PIO setup of ATAPI */
  153. PORT_CERR_SGT_BOUNDARY = 16, /* PLD ecode 00 - SGT not on qword boundary */
  154. PORT_CERR_SGT_TGTABRT = 17, /* PLD ecode 01 - target abort */
  155. PORT_CERR_SGT_MSTABRT = 18, /* PLD ecode 10 - master abort */
  156. PORT_CERR_SGT_PCIPERR = 19, /* PLD ecode 11 - PCI parity err while fetching SGT */
  157. PORT_CERR_CMD_BOUNDARY = 24, /* ctrl[15:13] 001 - PRB not on qword boundary */
  158. PORT_CERR_CMD_TGTABRT = 25, /* ctrl[15:13] 010 - target abort */
  159. PORT_CERR_CMD_MSTABRT = 26, /* ctrl[15:13] 100 - master abort */
  160. PORT_CERR_CMD_PCIPERR = 27, /* ctrl[15:13] 110 - PCI parity err while fetching PRB */
  161. PORT_CERR_XFR_UNDEF = 32, /* PSD ecode 00 - undefined */
  162. PORT_CERR_XFR_TGTABRT = 33, /* PSD ecode 01 - target abort */
  163. PORT_CERR_XFR_MSGABRT = 34, /* PSD ecode 10 - master abort */
  164. PORT_CERR_XFR_PCIPERR = 35, /* PSD ecode 11 - PCI prity err during transfer */
  165. PORT_CERR_SENDSERVICE = 36, /* FIS received while sending service */
  166. /* bits of PRB control field */
  167. PRB_CTRL_PROTOCOL = (1 << 0), /* override def. ATA protocol */
  168. PRB_CTRL_PACKET_READ = (1 << 4), /* PACKET cmd read */
  169. PRB_CTRL_PACKET_WRITE = (1 << 5), /* PACKET cmd write */
  170. PRB_CTRL_NIEN = (1 << 6), /* Mask completion irq */
  171. PRB_CTRL_SRST = (1 << 7), /* Soft reset request (ign BSY?) */
  172. /* PRB protocol field */
  173. PRB_PROT_PACKET = (1 << 0),
  174. PRB_PROT_TCQ = (1 << 1),
  175. PRB_PROT_NCQ = (1 << 2),
  176. PRB_PROT_READ = (1 << 3),
  177. PRB_PROT_WRITE = (1 << 4),
  178. PRB_PROT_TRANSPARENT = (1 << 5),
  179. /*
  180. * Other constants
  181. */
  182. SGE_TRM = (1 << 31), /* Last SGE in chain */
  183. SGE_LNK = (1 << 30), /* linked list
  184. Points to SGT, not SGE */
  185. SGE_DRD = (1 << 29), /* discard data read (/dev/null)
  186. data address ignored */
  187. /* board id */
  188. BID_SIL3124 = 0,
  189. BID_SIL3132 = 1,
  190. BID_SIL3131 = 2,
  191. IRQ_STAT_4PORTS = 0xf,
  192. };
  193. struct sil24_ata_block {
  194. struct sil24_prb prb;
  195. struct sil24_sge sge[LIBATA_MAX_PRD];
  196. };
  197. struct sil24_atapi_block {
  198. struct sil24_prb prb;
  199. u8 cdb[16];
  200. struct sil24_sge sge[LIBATA_MAX_PRD - 1];
  201. };
  202. union sil24_cmd_block {
  203. struct sil24_ata_block ata;
  204. struct sil24_atapi_block atapi;
  205. };
  206. /*
  207. * ap->private_data
  208. *
  209. * The preview driver always returned 0 for status. We emulate it
  210. * here from the previous interrupt.
  211. */
  212. struct sil24_port_priv {
  213. union sil24_cmd_block *cmd_block; /* 32 cmd blocks */
  214. dma_addr_t cmd_block_dma; /* DMA base addr for them */
  215. struct ata_taskfile tf; /* Cached taskfile registers */
  216. };
  217. /* ap->host_set->private_data */
  218. struct sil24_host_priv {
  219. void __iomem *host_base; /* global controller control (128 bytes @BAR0) */
  220. void __iomem *port_base; /* port registers (4 * 8192 bytes @BAR2) */
  221. };
  222. static void sil24_dev_config(struct ata_port *ap, struct ata_device *dev);
  223. static u8 sil24_check_status(struct ata_port *ap);
  224. static u32 sil24_scr_read(struct ata_port *ap, unsigned sc_reg);
  225. static void sil24_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val);
  226. static void sil24_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  227. static void sil24_phy_reset(struct ata_port *ap);
  228. static void sil24_qc_prep(struct ata_queued_cmd *qc);
  229. static unsigned int sil24_qc_issue(struct ata_queued_cmd *qc);
  230. static void sil24_irq_clear(struct ata_port *ap);
  231. static void sil24_eng_timeout(struct ata_port *ap);
  232. static irqreturn_t sil24_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
  233. static int sil24_port_start(struct ata_port *ap);
  234. static void sil24_port_stop(struct ata_port *ap);
  235. static void sil24_host_stop(struct ata_host_set *host_set);
  236. static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  237. static const struct pci_device_id sil24_pci_tbl[] = {
  238. { 0x1095, 0x3124, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3124 },
  239. { 0x1095, 0x3132, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3132 },
  240. { 0x1095, 0x3131, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3131 },
  241. { 0x1095, 0x3531, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3131 },
  242. { } /* terminate list */
  243. };
  244. static struct pci_driver sil24_pci_driver = {
  245. .name = DRV_NAME,
  246. .id_table = sil24_pci_tbl,
  247. .probe = sil24_init_one,
  248. .remove = ata_pci_remove_one, /* safe? */
  249. };
  250. static struct scsi_host_template sil24_sht = {
  251. .module = THIS_MODULE,
  252. .name = DRV_NAME,
  253. .ioctl = ata_scsi_ioctl,
  254. .queuecommand = ata_scsi_queuecmd,
  255. .eh_timed_out = ata_scsi_timed_out,
  256. .eh_strategy_handler = ata_scsi_error,
  257. .can_queue = ATA_DEF_QUEUE,
  258. .this_id = ATA_SHT_THIS_ID,
  259. .sg_tablesize = LIBATA_MAX_PRD,
  260. .max_sectors = ATA_MAX_SECTORS,
  261. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  262. .emulated = ATA_SHT_EMULATED,
  263. .use_clustering = ATA_SHT_USE_CLUSTERING,
  264. .proc_name = DRV_NAME,
  265. .dma_boundary = ATA_DMA_BOUNDARY,
  266. .slave_configure = ata_scsi_slave_config,
  267. .bios_param = ata_std_bios_param,
  268. };
  269. static const struct ata_port_operations sil24_ops = {
  270. .port_disable = ata_port_disable,
  271. .dev_config = sil24_dev_config,
  272. .check_status = sil24_check_status,
  273. .check_altstatus = sil24_check_status,
  274. .dev_select = ata_noop_dev_select,
  275. .tf_read = sil24_tf_read,
  276. .phy_reset = sil24_phy_reset,
  277. .qc_prep = sil24_qc_prep,
  278. .qc_issue = sil24_qc_issue,
  279. .eng_timeout = sil24_eng_timeout,
  280. .irq_handler = sil24_interrupt,
  281. .irq_clear = sil24_irq_clear,
  282. .scr_read = sil24_scr_read,
  283. .scr_write = sil24_scr_write,
  284. .port_start = sil24_port_start,
  285. .port_stop = sil24_port_stop,
  286. .host_stop = sil24_host_stop,
  287. };
  288. /*
  289. * Use bits 30-31 of host_flags to encode available port numbers.
  290. * Current maxium is 4.
  291. */
  292. #define SIL24_NPORTS2FLAG(nports) ((((unsigned)(nports) - 1) & 0x3) << 30)
  293. #define SIL24_FLAG2NPORTS(flag) ((((flag) >> 30) & 0x3) + 1)
  294. static struct ata_port_info sil24_port_info[] = {
  295. /* sil_3124 */
  296. {
  297. .sht = &sil24_sht,
  298. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  299. ATA_FLAG_SRST | ATA_FLAG_MMIO |
  300. ATA_FLAG_PIO_DMA | SIL24_NPORTS2FLAG(4),
  301. .pio_mask = 0x1f, /* pio0-4 */
  302. .mwdma_mask = 0x07, /* mwdma0-2 */
  303. .udma_mask = 0x3f, /* udma0-5 */
  304. .port_ops = &sil24_ops,
  305. },
  306. /* sil_3132 */
  307. {
  308. .sht = &sil24_sht,
  309. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  310. ATA_FLAG_SRST | ATA_FLAG_MMIO |
  311. ATA_FLAG_PIO_DMA | SIL24_NPORTS2FLAG(2),
  312. .pio_mask = 0x1f, /* pio0-4 */
  313. .mwdma_mask = 0x07, /* mwdma0-2 */
  314. .udma_mask = 0x3f, /* udma0-5 */
  315. .port_ops = &sil24_ops,
  316. },
  317. /* sil_3131/sil_3531 */
  318. {
  319. .sht = &sil24_sht,
  320. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  321. ATA_FLAG_SRST | ATA_FLAG_MMIO |
  322. ATA_FLAG_PIO_DMA | SIL24_NPORTS2FLAG(1),
  323. .pio_mask = 0x1f, /* pio0-4 */
  324. .mwdma_mask = 0x07, /* mwdma0-2 */
  325. .udma_mask = 0x3f, /* udma0-5 */
  326. .port_ops = &sil24_ops,
  327. },
  328. };
  329. static void sil24_dev_config(struct ata_port *ap, struct ata_device *dev)
  330. {
  331. void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
  332. if (ap->cdb_len == 16)
  333. writel(PORT_CS_CDB16, port + PORT_CTRL_STAT);
  334. else
  335. writel(PORT_CS_CDB16, port + PORT_CTRL_CLR);
  336. }
  337. static inline void sil24_update_tf(struct ata_port *ap)
  338. {
  339. struct sil24_port_priv *pp = ap->private_data;
  340. void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
  341. struct sil24_prb __iomem *prb = port;
  342. u8 fis[6 * 4];
  343. memcpy_fromio(fis, prb->fis, 6 * 4);
  344. ata_tf_from_fis(fis, &pp->tf);
  345. }
  346. static u8 sil24_check_status(struct ata_port *ap)
  347. {
  348. struct sil24_port_priv *pp = ap->private_data;
  349. return pp->tf.command;
  350. }
  351. static int sil24_scr_map[] = {
  352. [SCR_CONTROL] = 0,
  353. [SCR_STATUS] = 1,
  354. [SCR_ERROR] = 2,
  355. [SCR_ACTIVE] = 3,
  356. };
  357. static u32 sil24_scr_read(struct ata_port *ap, unsigned sc_reg)
  358. {
  359. void __iomem *scr_addr = (void __iomem *)ap->ioaddr.scr_addr;
  360. if (sc_reg < ARRAY_SIZE(sil24_scr_map)) {
  361. void __iomem *addr;
  362. addr = scr_addr + sil24_scr_map[sc_reg] * 4;
  363. return readl(scr_addr + sil24_scr_map[sc_reg] * 4);
  364. }
  365. return 0xffffffffU;
  366. }
  367. static void sil24_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val)
  368. {
  369. void __iomem *scr_addr = (void __iomem *)ap->ioaddr.scr_addr;
  370. if (sc_reg < ARRAY_SIZE(sil24_scr_map)) {
  371. void __iomem *addr;
  372. addr = scr_addr + sil24_scr_map[sc_reg] * 4;
  373. writel(val, scr_addr + sil24_scr_map[sc_reg] * 4);
  374. }
  375. }
  376. static void sil24_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  377. {
  378. struct sil24_port_priv *pp = ap->private_data;
  379. *tf = pp->tf;
  380. }
  381. static int sil24_issue_SRST(struct ata_port *ap)
  382. {
  383. void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
  384. struct sil24_port_priv *pp = ap->private_data;
  385. struct sil24_prb *prb = &pp->cmd_block[0].ata.prb;
  386. dma_addr_t paddr = pp->cmd_block_dma;
  387. u32 irq_enable, irq_stat;
  388. int cnt;
  389. /* temporarily turn off IRQs during SRST */
  390. irq_enable = readl(port + PORT_IRQ_ENABLE_SET);
  391. writel(irq_enable, port + PORT_IRQ_ENABLE_CLR);
  392. /*
  393. * XXX: Not sure whether the following sleep is needed or not.
  394. * The original driver had it. So....
  395. */
  396. msleep(10);
  397. prb->ctrl = PRB_CTRL_SRST;
  398. prb->fis[1] = 0; /* no PM yet */
  399. writel((u32)paddr, port + PORT_CMD_ACTIVATE);
  400. for (cnt = 0; cnt < 100; cnt++) {
  401. irq_stat = readl(port + PORT_IRQ_STAT);
  402. writel(irq_stat, port + PORT_IRQ_STAT); /* clear irq */
  403. irq_stat >>= PORT_IRQ_RAW_SHIFT;
  404. if (irq_stat & (PORT_IRQ_COMPLETE | PORT_IRQ_ERROR))
  405. break;
  406. msleep(1);
  407. }
  408. /* restore IRQs */
  409. writel(irq_enable, port + PORT_IRQ_ENABLE_SET);
  410. if (!(irq_stat & PORT_IRQ_COMPLETE))
  411. return -1;
  412. /* update TF */
  413. sil24_update_tf(ap);
  414. return 0;
  415. }
  416. static void sil24_phy_reset(struct ata_port *ap)
  417. {
  418. struct sil24_port_priv *pp = ap->private_data;
  419. __sata_phy_reset(ap);
  420. if (ap->flags & ATA_FLAG_PORT_DISABLED)
  421. return;
  422. if (sil24_issue_SRST(ap) < 0) {
  423. printk(KERN_ERR DRV_NAME
  424. " ata%u: SRST failed, disabling port\n", ap->id);
  425. ap->ops->port_disable(ap);
  426. return;
  427. }
  428. ap->device->class = ata_dev_classify(&pp->tf);
  429. }
  430. static inline void sil24_fill_sg(struct ata_queued_cmd *qc,
  431. struct sil24_sge *sge)
  432. {
  433. struct scatterlist *sg;
  434. unsigned int idx = 0;
  435. ata_for_each_sg(sg, qc) {
  436. sge->addr = cpu_to_le64(sg_dma_address(sg));
  437. sge->cnt = cpu_to_le32(sg_dma_len(sg));
  438. if (ata_sg_is_last(sg, qc))
  439. sge->flags = cpu_to_le32(SGE_TRM);
  440. else
  441. sge->flags = 0;
  442. sge++;
  443. idx++;
  444. }
  445. }
  446. static void sil24_qc_prep(struct ata_queued_cmd *qc)
  447. {
  448. struct ata_port *ap = qc->ap;
  449. struct sil24_port_priv *pp = ap->private_data;
  450. union sil24_cmd_block *cb = pp->cmd_block + qc->tag;
  451. struct sil24_prb *prb;
  452. struct sil24_sge *sge;
  453. switch (qc->tf.protocol) {
  454. case ATA_PROT_PIO:
  455. case ATA_PROT_DMA:
  456. case ATA_PROT_NODATA:
  457. prb = &cb->ata.prb;
  458. sge = cb->ata.sge;
  459. prb->ctrl = 0;
  460. break;
  461. case ATA_PROT_ATAPI:
  462. case ATA_PROT_ATAPI_DMA:
  463. case ATA_PROT_ATAPI_NODATA:
  464. prb = &cb->atapi.prb;
  465. sge = cb->atapi.sge;
  466. memset(cb->atapi.cdb, 0, 32);
  467. memcpy(cb->atapi.cdb, qc->cdb, ap->cdb_len);
  468. if (qc->tf.protocol != ATA_PROT_ATAPI_NODATA) {
  469. if (qc->tf.flags & ATA_TFLAG_WRITE)
  470. prb->ctrl = PRB_CTRL_PACKET_WRITE;
  471. else
  472. prb->ctrl = PRB_CTRL_PACKET_READ;
  473. } else
  474. prb->ctrl = 0;
  475. break;
  476. default:
  477. prb = NULL; /* shut up, gcc */
  478. sge = NULL;
  479. BUG();
  480. }
  481. ata_tf_to_fis(&qc->tf, prb->fis, 0);
  482. if (qc->flags & ATA_QCFLAG_DMAMAP)
  483. sil24_fill_sg(qc, sge);
  484. }
  485. static unsigned int sil24_qc_issue(struct ata_queued_cmd *qc)
  486. {
  487. struct ata_port *ap = qc->ap;
  488. void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
  489. struct sil24_port_priv *pp = ap->private_data;
  490. dma_addr_t paddr = pp->cmd_block_dma + qc->tag * sizeof(*pp->cmd_block);
  491. writel((u32)paddr, port + PORT_CMD_ACTIVATE);
  492. return 0;
  493. }
  494. static void sil24_irq_clear(struct ata_port *ap)
  495. {
  496. /* unused */
  497. }
  498. static int __sil24_restart_controller(void __iomem *port)
  499. {
  500. u32 tmp;
  501. int cnt;
  502. writel(PORT_CS_INIT, port + PORT_CTRL_STAT);
  503. /* Max ~10ms */
  504. for (cnt = 0; cnt < 10000; cnt++) {
  505. tmp = readl(port + PORT_CTRL_STAT);
  506. if (tmp & PORT_CS_RDY)
  507. return 0;
  508. udelay(1);
  509. }
  510. return -1;
  511. }
  512. static void sil24_restart_controller(struct ata_port *ap)
  513. {
  514. if (__sil24_restart_controller((void __iomem *)ap->ioaddr.cmd_addr))
  515. printk(KERN_ERR DRV_NAME
  516. " ata%u: failed to restart controller\n", ap->id);
  517. }
  518. static int __sil24_reset_controller(void __iomem *port)
  519. {
  520. int cnt;
  521. u32 tmp;
  522. /* Reset controller state. Is this correct? */
  523. writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT);
  524. readl(port + PORT_CTRL_STAT); /* sync */
  525. /* Max ~100ms */
  526. for (cnt = 0; cnt < 1000; cnt++) {
  527. udelay(100);
  528. tmp = readl(port + PORT_CTRL_STAT);
  529. if (!(tmp & PORT_CS_DEV_RST))
  530. break;
  531. }
  532. if (tmp & PORT_CS_DEV_RST)
  533. return -1;
  534. if (tmp & PORT_CS_RDY)
  535. return 0;
  536. return __sil24_restart_controller(port);
  537. }
  538. static void sil24_reset_controller(struct ata_port *ap)
  539. {
  540. printk(KERN_NOTICE DRV_NAME
  541. " ata%u: resetting controller...\n", ap->id);
  542. if (__sil24_reset_controller((void __iomem *)ap->ioaddr.cmd_addr))
  543. printk(KERN_ERR DRV_NAME
  544. " ata%u: failed to reset controller\n", ap->id);
  545. }
  546. static void sil24_eng_timeout(struct ata_port *ap)
  547. {
  548. struct ata_queued_cmd *qc;
  549. qc = ata_qc_from_tag(ap, ap->active_tag);
  550. printk(KERN_ERR "ata%u: command timeout\n", ap->id);
  551. qc->err_mask |= AC_ERR_TIMEOUT;
  552. ata_eh_qc_complete(qc);
  553. sil24_reset_controller(ap);
  554. }
  555. static void sil24_error_intr(struct ata_port *ap, u32 slot_stat)
  556. {
  557. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->active_tag);
  558. struct sil24_port_priv *pp = ap->private_data;
  559. void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
  560. u32 irq_stat, cmd_err, sstatus, serror;
  561. unsigned int err_mask;
  562. irq_stat = readl(port + PORT_IRQ_STAT);
  563. writel(irq_stat, port + PORT_IRQ_STAT); /* clear irq */
  564. if (!(irq_stat & PORT_IRQ_ERROR)) {
  565. /* ignore non-completion, non-error irqs for now */
  566. printk(KERN_WARNING DRV_NAME
  567. "ata%u: non-error exception irq (irq_stat %x)\n",
  568. ap->id, irq_stat);
  569. return;
  570. }
  571. cmd_err = readl(port + PORT_CMD_ERR);
  572. sstatus = readl(port + PORT_SSTATUS);
  573. serror = readl(port + PORT_SERROR);
  574. if (serror)
  575. writel(serror, port + PORT_SERROR);
  576. /*
  577. * Don't log ATAPI device errors. They're supposed to happen
  578. * and any serious errors will be logged using sense data by
  579. * the SCSI layer.
  580. */
  581. if (ap->device[0].class != ATA_DEV_ATAPI || cmd_err > PORT_CERR_SDB)
  582. printk("ata%u: error interrupt on port%d\n"
  583. " stat=0x%x irq=0x%x cmd_err=%d sstatus=0x%x serror=0x%x\n",
  584. ap->id, ap->port_no, slot_stat, irq_stat, cmd_err, sstatus, serror);
  585. if (cmd_err == PORT_CERR_DEV || cmd_err == PORT_CERR_SDB) {
  586. /*
  587. * Device is reporting error, tf registers are valid.
  588. */
  589. sil24_update_tf(ap);
  590. err_mask = ac_err_mask(pp->tf.command);
  591. sil24_restart_controller(ap);
  592. } else {
  593. /*
  594. * Other errors. libata currently doesn't have any
  595. * mechanism to report these errors. Just turn on
  596. * ATA_ERR.
  597. */
  598. err_mask = AC_ERR_OTHER;
  599. sil24_reset_controller(ap);
  600. }
  601. if (qc) {
  602. qc->err_mask |= err_mask;
  603. ata_qc_complete(qc);
  604. }
  605. }
  606. static inline void sil24_host_intr(struct ata_port *ap)
  607. {
  608. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->active_tag);
  609. void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
  610. u32 slot_stat;
  611. slot_stat = readl(port + PORT_SLOT_STAT);
  612. if (!(slot_stat & HOST_SSTAT_ATTN)) {
  613. struct sil24_port_priv *pp = ap->private_data;
  614. /*
  615. * !HOST_SSAT_ATTN guarantees successful completion,
  616. * so reading back tf registers is unnecessary for
  617. * most commands. TODO: read tf registers for
  618. * commands which require these values on successful
  619. * completion (EXECUTE DEVICE DIAGNOSTIC, CHECK POWER,
  620. * DEVICE RESET and READ PORT MULTIPLIER (any more?).
  621. */
  622. sil24_update_tf(ap);
  623. if (qc) {
  624. qc->err_mask |= ac_err_mask(pp->tf.command);
  625. ata_qc_complete(qc);
  626. }
  627. } else
  628. sil24_error_intr(ap, slot_stat);
  629. }
  630. static irqreturn_t sil24_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
  631. {
  632. struct ata_host_set *host_set = dev_instance;
  633. struct sil24_host_priv *hpriv = host_set->private_data;
  634. unsigned handled = 0;
  635. u32 status;
  636. int i;
  637. status = readl(hpriv->host_base + HOST_IRQ_STAT);
  638. if (status == 0xffffffff) {
  639. printk(KERN_ERR DRV_NAME ": IRQ status == 0xffffffff, "
  640. "PCI fault or device removal?\n");
  641. goto out;
  642. }
  643. if (!(status & IRQ_STAT_4PORTS))
  644. goto out;
  645. spin_lock(&host_set->lock);
  646. for (i = 0; i < host_set->n_ports; i++)
  647. if (status & (1 << i)) {
  648. struct ata_port *ap = host_set->ports[i];
  649. if (ap && !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
  650. sil24_host_intr(host_set->ports[i]);
  651. handled++;
  652. } else
  653. printk(KERN_ERR DRV_NAME
  654. ": interrupt from disabled port %d\n", i);
  655. }
  656. spin_unlock(&host_set->lock);
  657. out:
  658. return IRQ_RETVAL(handled);
  659. }
  660. static inline void sil24_cblk_free(struct sil24_port_priv *pp, struct device *dev)
  661. {
  662. const size_t cb_size = sizeof(*pp->cmd_block);
  663. dma_free_coherent(dev, cb_size, pp->cmd_block, pp->cmd_block_dma);
  664. }
  665. static int sil24_port_start(struct ata_port *ap)
  666. {
  667. struct device *dev = ap->host_set->dev;
  668. struct sil24_port_priv *pp;
  669. union sil24_cmd_block *cb;
  670. size_t cb_size = sizeof(*cb);
  671. dma_addr_t cb_dma;
  672. int rc = -ENOMEM;
  673. pp = kzalloc(sizeof(*pp), GFP_KERNEL);
  674. if (!pp)
  675. goto err_out;
  676. pp->tf.command = ATA_DRDY;
  677. cb = dma_alloc_coherent(dev, cb_size, &cb_dma, GFP_KERNEL);
  678. if (!cb)
  679. goto err_out_pp;
  680. memset(cb, 0, cb_size);
  681. rc = ata_pad_alloc(ap, dev);
  682. if (rc)
  683. goto err_out_pad;
  684. pp->cmd_block = cb;
  685. pp->cmd_block_dma = cb_dma;
  686. ap->private_data = pp;
  687. return 0;
  688. err_out_pad:
  689. sil24_cblk_free(pp, dev);
  690. err_out_pp:
  691. kfree(pp);
  692. err_out:
  693. return rc;
  694. }
  695. static void sil24_port_stop(struct ata_port *ap)
  696. {
  697. struct device *dev = ap->host_set->dev;
  698. struct sil24_port_priv *pp = ap->private_data;
  699. sil24_cblk_free(pp, dev);
  700. ata_pad_free(ap, dev);
  701. kfree(pp);
  702. }
  703. static void sil24_host_stop(struct ata_host_set *host_set)
  704. {
  705. struct sil24_host_priv *hpriv = host_set->private_data;
  706. iounmap(hpriv->host_base);
  707. iounmap(hpriv->port_base);
  708. kfree(hpriv);
  709. }
  710. static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  711. {
  712. static int printed_version = 0;
  713. unsigned int board_id = (unsigned int)ent->driver_data;
  714. struct ata_port_info *pinfo = &sil24_port_info[board_id];
  715. struct ata_probe_ent *probe_ent = NULL;
  716. struct sil24_host_priv *hpriv = NULL;
  717. void __iomem *host_base = NULL;
  718. void __iomem *port_base = NULL;
  719. int i, rc;
  720. if (!printed_version++)
  721. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  722. rc = pci_enable_device(pdev);
  723. if (rc)
  724. return rc;
  725. rc = pci_request_regions(pdev, DRV_NAME);
  726. if (rc)
  727. goto out_disable;
  728. rc = -ENOMEM;
  729. /* ioremap mmio registers */
  730. host_base = ioremap(pci_resource_start(pdev, 0),
  731. pci_resource_len(pdev, 0));
  732. if (!host_base)
  733. goto out_free;
  734. port_base = ioremap(pci_resource_start(pdev, 2),
  735. pci_resource_len(pdev, 2));
  736. if (!port_base)
  737. goto out_free;
  738. /* allocate & init probe_ent and hpriv */
  739. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  740. if (!probe_ent)
  741. goto out_free;
  742. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  743. if (!hpriv)
  744. goto out_free;
  745. memset(probe_ent, 0, sizeof(*probe_ent));
  746. probe_ent->dev = pci_dev_to_dev(pdev);
  747. INIT_LIST_HEAD(&probe_ent->node);
  748. probe_ent->sht = pinfo->sht;
  749. probe_ent->host_flags = pinfo->host_flags;
  750. probe_ent->pio_mask = pinfo->pio_mask;
  751. probe_ent->udma_mask = pinfo->udma_mask;
  752. probe_ent->port_ops = pinfo->port_ops;
  753. probe_ent->n_ports = SIL24_FLAG2NPORTS(pinfo->host_flags);
  754. probe_ent->irq = pdev->irq;
  755. probe_ent->irq_flags = SA_SHIRQ;
  756. probe_ent->mmio_base = port_base;
  757. probe_ent->private_data = hpriv;
  758. memset(hpriv, 0, sizeof(*hpriv));
  759. hpriv->host_base = host_base;
  760. hpriv->port_base = port_base;
  761. /*
  762. * Configure the device
  763. */
  764. /*
  765. * FIXME: This device is certainly 64-bit capable. We just
  766. * don't know how to use it. After fixing 32bit activation in
  767. * this function, enable 64bit masks here.
  768. */
  769. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  770. if (rc) {
  771. dev_printk(KERN_ERR, &pdev->dev,
  772. "32-bit DMA enable failed\n");
  773. goto out_free;
  774. }
  775. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  776. if (rc) {
  777. dev_printk(KERN_ERR, &pdev->dev,
  778. "32-bit consistent DMA enable failed\n");
  779. goto out_free;
  780. }
  781. /* GPIO off */
  782. writel(0, host_base + HOST_FLASH_CMD);
  783. /* Mask interrupts during initialization */
  784. writel(0, host_base + HOST_CTRL);
  785. for (i = 0; i < probe_ent->n_ports; i++) {
  786. void __iomem *port = port_base + i * PORT_REGS_SIZE;
  787. unsigned long portu = (unsigned long)port;
  788. u32 tmp;
  789. int cnt;
  790. probe_ent->port[i].cmd_addr = portu + PORT_PRB;
  791. probe_ent->port[i].scr_addr = portu + PORT_SCONTROL;
  792. ata_std_ports(&probe_ent->port[i]);
  793. /* Initial PHY setting */
  794. writel(0x20c, port + PORT_PHY_CFG);
  795. /* Clear port RST */
  796. tmp = readl(port + PORT_CTRL_STAT);
  797. if (tmp & PORT_CS_PORT_RST) {
  798. writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
  799. readl(port + PORT_CTRL_STAT); /* sync */
  800. for (cnt = 0; cnt < 10; cnt++) {
  801. msleep(10);
  802. tmp = readl(port + PORT_CTRL_STAT);
  803. if (!(tmp & PORT_CS_PORT_RST))
  804. break;
  805. }
  806. if (tmp & PORT_CS_PORT_RST)
  807. dev_printk(KERN_ERR, &pdev->dev,
  808. "failed to clear port RST\n");
  809. }
  810. /* Zero error counters. */
  811. writel(0x8000, port + PORT_DECODE_ERR_THRESH);
  812. writel(0x8000, port + PORT_CRC_ERR_THRESH);
  813. writel(0x8000, port + PORT_HSHK_ERR_THRESH);
  814. writel(0x0000, port + PORT_DECODE_ERR_CNT);
  815. writel(0x0000, port + PORT_CRC_ERR_CNT);
  816. writel(0x0000, port + PORT_HSHK_ERR_CNT);
  817. /* FIXME: 32bit activation? */
  818. writel(0, port + PORT_ACTIVATE_UPPER_ADDR);
  819. writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_STAT);
  820. /* Configure interrupts */
  821. writel(0xffff, port + PORT_IRQ_ENABLE_CLR);
  822. writel(PORT_IRQ_COMPLETE | PORT_IRQ_ERROR | PORT_IRQ_SDB_FIS,
  823. port + PORT_IRQ_ENABLE_SET);
  824. /* Clear interrupts */
  825. writel(0x0fff0fff, port + PORT_IRQ_STAT);
  826. writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR);
  827. /* Clear port multiplier enable and resume bits */
  828. writel(PORT_CS_PM_EN | PORT_CS_RESUME, port + PORT_CTRL_CLR);
  829. /* Reset itself */
  830. if (__sil24_reset_controller(port))
  831. dev_printk(KERN_ERR, &pdev->dev,
  832. "failed to reset controller\n");
  833. }
  834. /* Turn on interrupts */
  835. writel(IRQ_STAT_4PORTS, host_base + HOST_CTRL);
  836. pci_set_master(pdev);
  837. /* FIXME: check ata_device_add return value */
  838. ata_device_add(probe_ent);
  839. kfree(probe_ent);
  840. return 0;
  841. out_free:
  842. if (host_base)
  843. iounmap(host_base);
  844. if (port_base)
  845. iounmap(port_base);
  846. kfree(probe_ent);
  847. kfree(hpriv);
  848. pci_release_regions(pdev);
  849. out_disable:
  850. pci_disable_device(pdev);
  851. return rc;
  852. }
  853. static int __init sil24_init(void)
  854. {
  855. return pci_module_init(&sil24_pci_driver);
  856. }
  857. static void __exit sil24_exit(void)
  858. {
  859. pci_unregister_driver(&sil24_pci_driver);
  860. }
  861. MODULE_AUTHOR("Tejun Heo");
  862. MODULE_DESCRIPTION("Silicon Image 3124/3132 SATA low-level driver");
  863. MODULE_LICENSE("GPL");
  864. MODULE_DEVICE_TABLE(pci, sil24_pci_tbl);
  865. module_init(sil24_init);
  866. module_exit(sil24_exit);