sata_fsl.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. /*
  2. * drivers/ata/sata_fsl.c
  3. *
  4. * Freescale 3.0Gbps SATA device driver
  5. *
  6. * Author: Ashish Kalra <ashish.kalra@freescale.com>
  7. * Li Yang <leoli@freescale.com>
  8. *
  9. * Copyright (c) 2006-2007 Freescale Semiconductor, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <scsi/scsi_host.h>
  21. #include <scsi/scsi_cmnd.h>
  22. #include <linux/libata.h>
  23. #include <asm/io.h>
  24. #include <linux/of_platform.h>
  25. /* Controller information */
  26. enum {
  27. SATA_FSL_QUEUE_DEPTH = 16,
  28. SATA_FSL_MAX_PRD = 63,
  29. SATA_FSL_MAX_PRD_USABLE = SATA_FSL_MAX_PRD - 1,
  30. SATA_FSL_MAX_PRD_DIRECT = 16, /* Direct PRDT entries */
  31. SATA_FSL_HOST_FLAGS = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  32. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  33. ATA_FLAG_NCQ),
  34. SATA_FSL_MAX_CMDS = SATA_FSL_QUEUE_DEPTH,
  35. SATA_FSL_CMD_HDR_SIZE = 16, /* 4 DWORDS */
  36. SATA_FSL_CMD_SLOT_SIZE = (SATA_FSL_MAX_CMDS * SATA_FSL_CMD_HDR_SIZE),
  37. /*
  38. * SATA-FSL host controller supports a max. of (15+1) direct PRDEs, and
  39. * chained indirect PRDEs upto a max count of 63.
  40. * We are allocating an array of 63 PRDEs contigiously, but PRDE#15 will
  41. * be setup as an indirect descriptor, pointing to it's next
  42. * (contigious) PRDE. Though chained indirect PRDE arrays are
  43. * supported,it will be more efficient to use a direct PRDT and
  44. * a single chain/link to indirect PRDE array/PRDT.
  45. */
  46. SATA_FSL_CMD_DESC_CFIS_SZ = 32,
  47. SATA_FSL_CMD_DESC_SFIS_SZ = 32,
  48. SATA_FSL_CMD_DESC_ACMD_SZ = 16,
  49. SATA_FSL_CMD_DESC_RSRVD = 16,
  50. SATA_FSL_CMD_DESC_SIZE = (SATA_FSL_CMD_DESC_CFIS_SZ +
  51. SATA_FSL_CMD_DESC_SFIS_SZ +
  52. SATA_FSL_CMD_DESC_ACMD_SZ +
  53. SATA_FSL_CMD_DESC_RSRVD +
  54. SATA_FSL_MAX_PRD * 16),
  55. SATA_FSL_CMD_DESC_OFFSET_TO_PRDT =
  56. (SATA_FSL_CMD_DESC_CFIS_SZ +
  57. SATA_FSL_CMD_DESC_SFIS_SZ +
  58. SATA_FSL_CMD_DESC_ACMD_SZ +
  59. SATA_FSL_CMD_DESC_RSRVD),
  60. SATA_FSL_CMD_DESC_AR_SZ = (SATA_FSL_CMD_DESC_SIZE * SATA_FSL_MAX_CMDS),
  61. SATA_FSL_PORT_PRIV_DMA_SZ = (SATA_FSL_CMD_SLOT_SIZE +
  62. SATA_FSL_CMD_DESC_AR_SZ),
  63. /*
  64. * MPC8315 has two SATA controllers, SATA1 & SATA2
  65. * (one port per controller)
  66. * MPC837x has 2/4 controllers, one port per controller
  67. */
  68. SATA_FSL_MAX_PORTS = 1,
  69. SATA_FSL_IRQ_FLAG = IRQF_SHARED,
  70. };
  71. /*
  72. * Host Controller command register set - per port
  73. */
  74. enum {
  75. CQ = 0,
  76. CA = 8,
  77. CC = 0x10,
  78. CE = 0x18,
  79. DE = 0x20,
  80. CHBA = 0x24,
  81. HSTATUS = 0x28,
  82. HCONTROL = 0x2C,
  83. CQPMP = 0x30,
  84. SIGNATURE = 0x34,
  85. ICC = 0x38,
  86. /*
  87. * Host Status Register (HStatus) bitdefs
  88. */
  89. ONLINE = (1 << 31),
  90. GOING_OFFLINE = (1 << 30),
  91. BIST_ERR = (1 << 29),
  92. FATAL_ERR_HC_MASTER_ERR = (1 << 18),
  93. FATAL_ERR_PARITY_ERR_TX = (1 << 17),
  94. FATAL_ERR_PARITY_ERR_RX = (1 << 16),
  95. FATAL_ERR_DATA_UNDERRUN = (1 << 13),
  96. FATAL_ERR_DATA_OVERRUN = (1 << 12),
  97. FATAL_ERR_CRC_ERR_TX = (1 << 11),
  98. FATAL_ERR_CRC_ERR_RX = (1 << 10),
  99. FATAL_ERR_FIFO_OVRFL_TX = (1 << 9),
  100. FATAL_ERR_FIFO_OVRFL_RX = (1 << 8),
  101. FATAL_ERROR_DECODE = FATAL_ERR_HC_MASTER_ERR |
  102. FATAL_ERR_PARITY_ERR_TX |
  103. FATAL_ERR_PARITY_ERR_RX |
  104. FATAL_ERR_DATA_UNDERRUN |
  105. FATAL_ERR_DATA_OVERRUN |
  106. FATAL_ERR_CRC_ERR_TX |
  107. FATAL_ERR_CRC_ERR_RX |
  108. FATAL_ERR_FIFO_OVRFL_TX | FATAL_ERR_FIFO_OVRFL_RX,
  109. INT_ON_FATAL_ERR = (1 << 5),
  110. INT_ON_PHYRDY_CHG = (1 << 4),
  111. INT_ON_SIGNATURE_UPDATE = (1 << 3),
  112. INT_ON_SNOTIFY_UPDATE = (1 << 2),
  113. INT_ON_SINGL_DEVICE_ERR = (1 << 1),
  114. INT_ON_CMD_COMPLETE = 1,
  115. INT_ON_ERROR = INT_ON_FATAL_ERR |
  116. INT_ON_PHYRDY_CHG | INT_ON_SINGL_DEVICE_ERR,
  117. /*
  118. * Host Control Register (HControl) bitdefs
  119. */
  120. HCONTROL_ONLINE_PHY_RST = (1 << 31),
  121. HCONTROL_FORCE_OFFLINE = (1 << 30),
  122. HCONTROL_PARITY_PROT_MOD = (1 << 14),
  123. HCONTROL_DPATH_PARITY = (1 << 12),
  124. HCONTROL_SNOOP_ENABLE = (1 << 10),
  125. HCONTROL_PMP_ATTACHED = (1 << 9),
  126. HCONTROL_COPYOUT_STATFIS = (1 << 8),
  127. IE_ON_FATAL_ERR = (1 << 5),
  128. IE_ON_PHYRDY_CHG = (1 << 4),
  129. IE_ON_SIGNATURE_UPDATE = (1 << 3),
  130. IE_ON_SNOTIFY_UPDATE = (1 << 2),
  131. IE_ON_SINGL_DEVICE_ERR = (1 << 1),
  132. IE_ON_CMD_COMPLETE = 1,
  133. DEFAULT_PORT_IRQ_ENABLE_MASK = IE_ON_FATAL_ERR | IE_ON_PHYRDY_CHG |
  134. IE_ON_SIGNATURE_UPDATE |
  135. IE_ON_SINGL_DEVICE_ERR | IE_ON_CMD_COMPLETE,
  136. EXT_INDIRECT_SEG_PRD_FLAG = (1 << 31),
  137. DATA_SNOOP_ENABLE = (1 << 22),
  138. };
  139. /*
  140. * SATA Superset Registers
  141. */
  142. enum {
  143. SSTATUS = 0,
  144. SERROR = 4,
  145. SCONTROL = 8,
  146. SNOTIFY = 0xC,
  147. };
  148. /*
  149. * Control Status Register Set
  150. */
  151. enum {
  152. TRANSCFG = 0,
  153. TRANSSTATUS = 4,
  154. LINKCFG = 8,
  155. LINKCFG1 = 0xC,
  156. LINKCFG2 = 0x10,
  157. LINKSTATUS = 0x14,
  158. LINKSTATUS1 = 0x18,
  159. PHYCTRLCFG = 0x1C,
  160. COMMANDSTAT = 0x20,
  161. };
  162. /* PHY (link-layer) configuration control */
  163. enum {
  164. PHY_BIST_ENABLE = 0x01,
  165. };
  166. /*
  167. * Command Header Table entry, i.e, command slot
  168. * 4 Dwords per command slot, command header size == 64 Dwords.
  169. */
  170. struct cmdhdr_tbl_entry {
  171. u32 cda;
  172. u32 prde_fis_len;
  173. u32 ttl;
  174. u32 desc_info;
  175. };
  176. /*
  177. * Description information bitdefs
  178. */
  179. enum {
  180. VENDOR_SPECIFIC_BIST = (1 << 10),
  181. CMD_DESC_SNOOP_ENABLE = (1 << 9),
  182. FPDMA_QUEUED_CMD = (1 << 8),
  183. SRST_CMD = (1 << 7),
  184. BIST = (1 << 6),
  185. ATAPI_CMD = (1 << 5),
  186. };
  187. /*
  188. * Command Descriptor
  189. */
  190. struct command_desc {
  191. u8 cfis[8 * 4];
  192. u8 sfis[8 * 4];
  193. u8 acmd[4 * 4];
  194. u8 fill[4 * 4];
  195. u32 prdt[SATA_FSL_MAX_PRD_DIRECT * 4];
  196. u32 prdt_indirect[(SATA_FSL_MAX_PRD - SATA_FSL_MAX_PRD_DIRECT) * 4];
  197. };
  198. /*
  199. * Physical region table descriptor(PRD)
  200. */
  201. struct prde {
  202. u32 dba;
  203. u8 fill[2 * 4];
  204. u32 ddc_and_ext;
  205. };
  206. /*
  207. * ata_port private data
  208. * This is our per-port instance data.
  209. */
  210. struct sata_fsl_port_priv {
  211. struct cmdhdr_tbl_entry *cmdslot;
  212. dma_addr_t cmdslot_paddr;
  213. struct command_desc *cmdentry;
  214. dma_addr_t cmdentry_paddr;
  215. /*
  216. * SATA FSL controller has a Status FIS which should contain the
  217. * received D2H FIS & taskfile registers. This SFIS is present in
  218. * the command descriptor, and to have a ready reference to it,
  219. * we are caching it here, quite similar to what is done in H/W on
  220. * AHCI compliant devices by copying taskfile fields to a 32-bit
  221. * register.
  222. */
  223. struct ata_taskfile tf;
  224. };
  225. /*
  226. * ata_port->host_set private data
  227. */
  228. struct sata_fsl_host_priv {
  229. void __iomem *hcr_base;
  230. void __iomem *ssr_base;
  231. void __iomem *csr_base;
  232. int irq;
  233. };
  234. static inline unsigned int sata_fsl_tag(unsigned int tag,
  235. void __iomem *hcr_base)
  236. {
  237. /* We let libATA core do actual (queue) tag allocation */
  238. /* all non NCQ/queued commands should have tag#0 */
  239. if (ata_tag_internal(tag)) {
  240. DPRINTK("mapping internal cmds to tag#0\n");
  241. return 0;
  242. }
  243. if (unlikely(tag >= SATA_FSL_QUEUE_DEPTH)) {
  244. DPRINTK("tag %d invalid : out of range\n", tag);
  245. return 0;
  246. }
  247. if (unlikely((ioread32(hcr_base + CQ)) & (1 << tag))) {
  248. DPRINTK("tag %d invalid : in use!!\n", tag);
  249. return 0;
  250. }
  251. return tag;
  252. }
  253. static void sata_fsl_setup_cmd_hdr_entry(struct sata_fsl_port_priv *pp,
  254. unsigned int tag, u32 desc_info,
  255. u32 data_xfer_len, u8 num_prde,
  256. u8 fis_len)
  257. {
  258. dma_addr_t cmd_descriptor_address;
  259. cmd_descriptor_address = pp->cmdentry_paddr +
  260. tag * SATA_FSL_CMD_DESC_SIZE;
  261. /* NOTE: both data_xfer_len & fis_len are Dword counts */
  262. pp->cmdslot[tag].cda = cpu_to_le32(cmd_descriptor_address);
  263. pp->cmdslot[tag].prde_fis_len =
  264. cpu_to_le32((num_prde << 16) | (fis_len << 2));
  265. pp->cmdslot[tag].ttl = cpu_to_le32(data_xfer_len & ~0x03);
  266. pp->cmdslot[tag].desc_info = cpu_to_le32(desc_info | (tag & 0x1F));
  267. VPRINTK("cda=0x%x, prde_fis_len=0x%x, ttl=0x%x, di=0x%x\n",
  268. pp->cmdslot[tag].cda,
  269. pp->cmdslot[tag].prde_fis_len,
  270. pp->cmdslot[tag].ttl, pp->cmdslot[tag].desc_info);
  271. }
  272. static unsigned int sata_fsl_fill_sg(struct ata_queued_cmd *qc, void *cmd_desc,
  273. u32 *ttl, dma_addr_t cmd_desc_paddr)
  274. {
  275. struct scatterlist *sg;
  276. unsigned int num_prde = 0;
  277. u32 ttl_dwords = 0;
  278. /*
  279. * NOTE : direct & indirect prdt's are contigiously allocated
  280. */
  281. struct prde *prd = (struct prde *)&((struct command_desc *)
  282. cmd_desc)->prdt;
  283. struct prde *prd_ptr_to_indirect_ext = NULL;
  284. unsigned indirect_ext_segment_sz = 0;
  285. dma_addr_t indirect_ext_segment_paddr;
  286. unsigned int si;
  287. VPRINTK("SATA FSL : cd = 0x%p, prd = 0x%p\n", cmd_desc, prd);
  288. indirect_ext_segment_paddr = cmd_desc_paddr +
  289. SATA_FSL_CMD_DESC_OFFSET_TO_PRDT + SATA_FSL_MAX_PRD_DIRECT * 16;
  290. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  291. dma_addr_t sg_addr = sg_dma_address(sg);
  292. u32 sg_len = sg_dma_len(sg);
  293. VPRINTK("SATA FSL : fill_sg, sg_addr = 0x%x, sg_len = %d\n",
  294. sg_addr, sg_len);
  295. /* warn if each s/g element is not dword aligned */
  296. if (sg_addr & 0x03)
  297. ata_port_printk(qc->ap, KERN_ERR,
  298. "s/g addr unaligned : 0x%x\n", sg_addr);
  299. if (sg_len & 0x03)
  300. ata_port_printk(qc->ap, KERN_ERR,
  301. "s/g len unaligned : 0x%x\n", sg_len);
  302. if (num_prde == (SATA_FSL_MAX_PRD_DIRECT - 1) &&
  303. sg_next(sg) != NULL) {
  304. VPRINTK("setting indirect prde\n");
  305. prd_ptr_to_indirect_ext = prd;
  306. prd->dba = cpu_to_le32(indirect_ext_segment_paddr);
  307. indirect_ext_segment_sz = 0;
  308. ++prd;
  309. ++num_prde;
  310. }
  311. ttl_dwords += sg_len;
  312. prd->dba = cpu_to_le32(sg_addr);
  313. prd->ddc_and_ext =
  314. cpu_to_le32(DATA_SNOOP_ENABLE | (sg_len & ~0x03));
  315. VPRINTK("sg_fill, ttl=%d, dba=0x%x, ddc=0x%x\n",
  316. ttl_dwords, prd->dba, prd->ddc_and_ext);
  317. ++num_prde;
  318. ++prd;
  319. if (prd_ptr_to_indirect_ext)
  320. indirect_ext_segment_sz += sg_len;
  321. }
  322. if (prd_ptr_to_indirect_ext) {
  323. /* set indirect extension flag along with indirect ext. size */
  324. prd_ptr_to_indirect_ext->ddc_and_ext =
  325. cpu_to_le32((EXT_INDIRECT_SEG_PRD_FLAG |
  326. DATA_SNOOP_ENABLE |
  327. (indirect_ext_segment_sz & ~0x03)));
  328. }
  329. *ttl = ttl_dwords;
  330. return num_prde;
  331. }
  332. static void sata_fsl_qc_prep(struct ata_queued_cmd *qc)
  333. {
  334. struct ata_port *ap = qc->ap;
  335. struct sata_fsl_port_priv *pp = ap->private_data;
  336. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  337. void __iomem *hcr_base = host_priv->hcr_base;
  338. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  339. struct command_desc *cd;
  340. u32 desc_info = CMD_DESC_SNOOP_ENABLE;
  341. u32 num_prde = 0;
  342. u32 ttl_dwords = 0;
  343. dma_addr_t cd_paddr;
  344. cd = (struct command_desc *)pp->cmdentry + tag;
  345. cd_paddr = pp->cmdentry_paddr + tag * SATA_FSL_CMD_DESC_SIZE;
  346. ata_tf_to_fis(&qc->tf, 0, 1, (u8 *) &cd->cfis);
  347. VPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x\n",
  348. cd->cfis[0], cd->cfis[1], cd->cfis[2]);
  349. if (qc->tf.protocol == ATA_PROT_NCQ) {
  350. VPRINTK("FPDMA xfer,Sctor cnt[0:7],[8:15] = %d,%d\n",
  351. cd->cfis[3], cd->cfis[11]);
  352. }
  353. /* setup "ACMD - atapi command" in cmd. desc. if this is ATAPI cmd */
  354. if (ata_is_atapi(qc->tf.protocol)) {
  355. desc_info |= ATAPI_CMD;
  356. memset((void *)&cd->acmd, 0, 32);
  357. memcpy((void *)&cd->acmd, qc->cdb, qc->dev->cdb_len);
  358. }
  359. if (qc->flags & ATA_QCFLAG_DMAMAP)
  360. num_prde = sata_fsl_fill_sg(qc, (void *)cd,
  361. &ttl_dwords, cd_paddr);
  362. if (qc->tf.protocol == ATA_PROT_NCQ)
  363. desc_info |= FPDMA_QUEUED_CMD;
  364. sata_fsl_setup_cmd_hdr_entry(pp, tag, desc_info, ttl_dwords,
  365. num_prde, 5);
  366. VPRINTK("SATA FSL : xx_qc_prep, di = 0x%x, ttl = %d, num_prde = %d\n",
  367. desc_info, ttl_dwords, num_prde);
  368. }
  369. static unsigned int sata_fsl_qc_issue(struct ata_queued_cmd *qc)
  370. {
  371. struct ata_port *ap = qc->ap;
  372. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  373. void __iomem *hcr_base = host_priv->hcr_base;
  374. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  375. VPRINTK("xx_qc_issue called,CQ=0x%x,CA=0x%x,CE=0x%x,CC=0x%x\n",
  376. ioread32(CQ + hcr_base),
  377. ioread32(CA + hcr_base),
  378. ioread32(CE + hcr_base), ioread32(CC + hcr_base));
  379. /* Simply queue command to the controller/device */
  380. iowrite32(1 << tag, CQ + hcr_base);
  381. VPRINTK("xx_qc_issue called, tag=%d, CQ=0x%x, CA=0x%x\n",
  382. tag, ioread32(CQ + hcr_base), ioread32(CA + hcr_base));
  383. VPRINTK("CE=0x%x, DE=0x%x, CC=0x%x, CmdStat = 0x%x\n",
  384. ioread32(CE + hcr_base),
  385. ioread32(DE + hcr_base),
  386. ioread32(CC + hcr_base),
  387. ioread32(COMMANDSTAT + host_priv->csr_base));
  388. return 0;
  389. }
  390. static bool sata_fsl_qc_fill_rtf(struct ata_queued_cmd *qc)
  391. {
  392. struct sata_fsl_port_priv *pp = qc->ap->private_data;
  393. struct sata_fsl_host_priv *host_priv = qc->ap->host->private_data;
  394. void __iomem *hcr_base = host_priv->hcr_base;
  395. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  396. struct command_desc *cd;
  397. cd = pp->cmdentry + tag;
  398. ata_tf_from_fis(cd->sfis, &qc->result_tf);
  399. return true;
  400. }
  401. static int sata_fsl_scr_write(struct ata_port *ap, unsigned int sc_reg_in,
  402. u32 val)
  403. {
  404. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  405. void __iomem *ssr_base = host_priv->ssr_base;
  406. unsigned int sc_reg;
  407. switch (sc_reg_in) {
  408. case SCR_STATUS:
  409. case SCR_ERROR:
  410. case SCR_CONTROL:
  411. case SCR_ACTIVE:
  412. sc_reg = sc_reg_in;
  413. break;
  414. default:
  415. return -EINVAL;
  416. }
  417. VPRINTK("xx_scr_write, reg_in = %d\n", sc_reg);
  418. iowrite32(val, ssr_base + (sc_reg * 4));
  419. return 0;
  420. }
  421. static int sata_fsl_scr_read(struct ata_port *ap, unsigned int sc_reg_in,
  422. u32 *val)
  423. {
  424. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  425. void __iomem *ssr_base = host_priv->ssr_base;
  426. unsigned int sc_reg;
  427. switch (sc_reg_in) {
  428. case SCR_STATUS:
  429. case SCR_ERROR:
  430. case SCR_CONTROL:
  431. case SCR_ACTIVE:
  432. sc_reg = sc_reg_in;
  433. break;
  434. default:
  435. return -EINVAL;
  436. }
  437. VPRINTK("xx_scr_read, reg_in = %d\n", sc_reg);
  438. *val = ioread32(ssr_base + (sc_reg * 4));
  439. return 0;
  440. }
  441. static void sata_fsl_freeze(struct ata_port *ap)
  442. {
  443. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  444. void __iomem *hcr_base = host_priv->hcr_base;
  445. u32 temp;
  446. VPRINTK("xx_freeze, CQ=0x%x, CA=0x%x, CE=0x%x, DE=0x%x\n",
  447. ioread32(CQ + hcr_base),
  448. ioread32(CA + hcr_base),
  449. ioread32(CE + hcr_base), ioread32(DE + hcr_base));
  450. VPRINTK("CmdStat = 0x%x\n",
  451. ioread32(host_priv->csr_base + COMMANDSTAT));
  452. /* disable interrupts on the controller/port */
  453. temp = ioread32(hcr_base + HCONTROL);
  454. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  455. VPRINTK("in xx_freeze : HControl = 0x%x, HStatus = 0x%x\n",
  456. ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
  457. }
  458. static void sata_fsl_thaw(struct ata_port *ap)
  459. {
  460. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  461. void __iomem *hcr_base = host_priv->hcr_base;
  462. u32 temp;
  463. /* ack. any pending IRQs for this controller/port */
  464. temp = ioread32(hcr_base + HSTATUS);
  465. VPRINTK("xx_thaw, pending IRQs = 0x%x\n", (temp & 0x3F));
  466. if (temp & 0x3F)
  467. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  468. /* enable interrupts on the controller/port */
  469. temp = ioread32(hcr_base + HCONTROL);
  470. iowrite32((temp | DEFAULT_PORT_IRQ_ENABLE_MASK), hcr_base + HCONTROL);
  471. VPRINTK("xx_thaw : HControl = 0x%x, HStatus = 0x%x\n",
  472. ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
  473. }
  474. /*
  475. * NOTE : 1st D2H FIS from device does not update sfis in command descriptor.
  476. */
  477. static inline void sata_fsl_cache_taskfile_from_d2h_fis(struct ata_queued_cmd
  478. *qc,
  479. struct ata_port *ap)
  480. {
  481. struct sata_fsl_port_priv *pp = ap->private_data;
  482. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  483. void __iomem *hcr_base = host_priv->hcr_base;
  484. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  485. struct command_desc *cd;
  486. cd = pp->cmdentry + tag;
  487. ata_tf_from_fis(cd->sfis, &pp->tf);
  488. }
  489. static u8 sata_fsl_check_status(struct ata_port *ap)
  490. {
  491. struct sata_fsl_port_priv *pp = ap->private_data;
  492. return pp->tf.command;
  493. }
  494. static int sata_fsl_port_start(struct ata_port *ap)
  495. {
  496. struct device *dev = ap->host->dev;
  497. struct sata_fsl_port_priv *pp;
  498. int retval;
  499. void *mem;
  500. dma_addr_t mem_dma;
  501. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  502. void __iomem *hcr_base = host_priv->hcr_base;
  503. u32 temp;
  504. pp = kzalloc(sizeof(*pp), GFP_KERNEL);
  505. if (!pp)
  506. return -ENOMEM;
  507. mem = dma_alloc_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ, &mem_dma,
  508. GFP_KERNEL);
  509. if (!mem) {
  510. kfree(pp);
  511. return -ENOMEM;
  512. }
  513. memset(mem, 0, SATA_FSL_PORT_PRIV_DMA_SZ);
  514. pp->cmdslot = mem;
  515. pp->cmdslot_paddr = mem_dma;
  516. mem += SATA_FSL_CMD_SLOT_SIZE;
  517. mem_dma += SATA_FSL_CMD_SLOT_SIZE;
  518. pp->cmdentry = mem;
  519. pp->cmdentry_paddr = mem_dma;
  520. ap->private_data = pp;
  521. VPRINTK("CHBA = 0x%x, cmdentry_phys = 0x%x\n",
  522. pp->cmdslot_paddr, pp->cmdentry_paddr);
  523. /* Now, update the CHBA register in host controller cmd register set */
  524. iowrite32(pp->cmdslot_paddr & 0xffffffff, hcr_base + CHBA);
  525. /*
  526. * Now, we can bring the controller on-line & also initiate
  527. * the COMINIT sequence, we simply return here and the boot-probing
  528. * & device discovery process is re-initiated by libATA using a
  529. * Softreset EH (dummy) session. Hence, boot probing and device
  530. * discovey will be part of sata_fsl_softreset() callback.
  531. */
  532. temp = ioread32(hcr_base + HCONTROL);
  533. iowrite32((temp | HCONTROL_ONLINE_PHY_RST), hcr_base + HCONTROL);
  534. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  535. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  536. VPRINTK("CHBA = 0x%x\n", ioread32(hcr_base + CHBA));
  537. #ifdef CONFIG_MPC8315_DS
  538. /*
  539. * Workaround for 8315DS board 3gbps link-up issue,
  540. * currently limit SATA port to GEN1 speed
  541. */
  542. sata_fsl_scr_read(ap, SCR_CONTROL, &temp);
  543. temp &= ~(0xF << 4);
  544. temp |= (0x1 << 4);
  545. sata_fsl_scr_write(ap, SCR_CONTROL, temp);
  546. sata_fsl_scr_read(ap, SCR_CONTROL, &temp);
  547. dev_printk(KERN_WARNING, dev, "scr_control, speed limited to %x\n",
  548. temp);
  549. #endif
  550. return 0;
  551. }
  552. static void sata_fsl_port_stop(struct ata_port *ap)
  553. {
  554. struct device *dev = ap->host->dev;
  555. struct sata_fsl_port_priv *pp = ap->private_data;
  556. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  557. void __iomem *hcr_base = host_priv->hcr_base;
  558. u32 temp;
  559. /*
  560. * Force host controller to go off-line, aborting current operations
  561. */
  562. temp = ioread32(hcr_base + HCONTROL);
  563. temp &= ~HCONTROL_ONLINE_PHY_RST;
  564. temp |= HCONTROL_FORCE_OFFLINE;
  565. iowrite32(temp, hcr_base + HCONTROL);
  566. /* Poll for controller to go offline - should happen immediately */
  567. ata_wait_register(hcr_base + HSTATUS, ONLINE, ONLINE, 1, 1);
  568. ap->private_data = NULL;
  569. dma_free_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ,
  570. pp->cmdslot, pp->cmdslot_paddr);
  571. kfree(pp);
  572. }
  573. static unsigned int sata_fsl_dev_classify(struct ata_port *ap)
  574. {
  575. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  576. void __iomem *hcr_base = host_priv->hcr_base;
  577. struct ata_taskfile tf;
  578. u32 temp;
  579. temp = ioread32(hcr_base + SIGNATURE);
  580. VPRINTK("raw sig = 0x%x\n", temp);
  581. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  582. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  583. tf.lbah = (temp >> 24) & 0xff;
  584. tf.lbam = (temp >> 16) & 0xff;
  585. tf.lbal = (temp >> 8) & 0xff;
  586. tf.nsect = temp & 0xff;
  587. return ata_dev_classify(&tf);
  588. }
  589. static int sata_fsl_softreset(struct ata_link *link, unsigned int *class,
  590. unsigned long deadline)
  591. {
  592. struct ata_port *ap = link->ap;
  593. struct sata_fsl_port_priv *pp = ap->private_data;
  594. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  595. void __iomem *hcr_base = host_priv->hcr_base;
  596. u32 temp;
  597. struct ata_taskfile tf;
  598. u8 *cfis;
  599. u32 Serror;
  600. int i = 0;
  601. unsigned long start_jiffies;
  602. DPRINTK("in xx_softreset\n");
  603. try_offline_again:
  604. /*
  605. * Force host controller to go off-line, aborting current operations
  606. */
  607. temp = ioread32(hcr_base + HCONTROL);
  608. temp &= ~HCONTROL_ONLINE_PHY_RST;
  609. iowrite32(temp, hcr_base + HCONTROL);
  610. /* Poll for controller to go offline */
  611. temp = ata_wait_register(hcr_base + HSTATUS, ONLINE, ONLINE, 1, 500);
  612. if (temp & ONLINE) {
  613. ata_port_printk(ap, KERN_ERR,
  614. "Softreset failed, not off-lined %d\n", i);
  615. /*
  616. * Try to offline controller atleast twice
  617. */
  618. i++;
  619. if (i == 2)
  620. goto err;
  621. else
  622. goto try_offline_again;
  623. }
  624. DPRINTK("softreset, controller off-lined\n");
  625. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  626. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  627. /*
  628. * PHY reset should remain asserted for atleast 1ms
  629. */
  630. msleep(1);
  631. /*
  632. * Now, bring the host controller online again, this can take time
  633. * as PHY reset and communication establishment, 1st D2H FIS and
  634. * device signature update is done, on safe side assume 500ms
  635. * NOTE : Host online status may be indicated immediately!!
  636. */
  637. temp = ioread32(hcr_base + HCONTROL);
  638. temp |= (HCONTROL_ONLINE_PHY_RST | HCONTROL_SNOOP_ENABLE);
  639. iowrite32(temp, hcr_base + HCONTROL);
  640. temp = ata_wait_register(hcr_base + HSTATUS, ONLINE, 0, 1, 500);
  641. if (!(temp & ONLINE)) {
  642. ata_port_printk(ap, KERN_ERR,
  643. "Softreset failed, not on-lined\n");
  644. goto err;
  645. }
  646. DPRINTK("softreset, controller off-lined & on-lined\n");
  647. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  648. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  649. /*
  650. * First, wait for the PHYRDY change to occur before waiting for
  651. * the signature, and also verify if SStatus indicates device
  652. * presence
  653. */
  654. temp = ata_wait_register(hcr_base + HSTATUS, 0xFF, 0, 1, 500);
  655. if ((!(temp & 0x10)) || ata_link_offline(link)) {
  656. ata_port_printk(ap, KERN_WARNING,
  657. "No Device OR PHYRDY change,Hstatus = 0x%x\n",
  658. ioread32(hcr_base + HSTATUS));
  659. goto err;
  660. }
  661. /*
  662. * Wait for the first D2H from device,i.e,signature update notification
  663. */
  664. start_jiffies = jiffies;
  665. temp = ata_wait_register(hcr_base + HSTATUS, 0xFF, 0x10,
  666. 500, jiffies_to_msecs(deadline - start_jiffies));
  667. if ((temp & 0xFF) != 0x18) {
  668. ata_port_printk(ap, KERN_WARNING, "No Signature Update\n");
  669. goto err;
  670. } else {
  671. ata_port_printk(ap, KERN_INFO,
  672. "Signature Update detected @ %d msecs\n",
  673. jiffies_to_msecs(jiffies - start_jiffies));
  674. }
  675. /*
  676. * Send a device reset (SRST) explicitly on command slot #0
  677. * Check : will the command queue (reg) be cleared during offlining ??
  678. * Also we will be online only if Phy commn. has been established
  679. * and device presence has been detected, therefore if we have
  680. * reached here, we can send a command to the target device
  681. */
  682. DPRINTK("Sending SRST/device reset\n");
  683. ata_tf_init(link->device, &tf);
  684. cfis = (u8 *) &pp->cmdentry->cfis;
  685. /* device reset/SRST is a control register update FIS, uses tag0 */
  686. sata_fsl_setup_cmd_hdr_entry(pp, 0,
  687. SRST_CMD | CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
  688. tf.ctl |= ATA_SRST; /* setup SRST bit in taskfile control reg */
  689. ata_tf_to_fis(&tf, 0, 0, cfis);
  690. DPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x, 0x%x\n",
  691. cfis[0], cfis[1], cfis[2], cfis[3]);
  692. /*
  693. * Queue SRST command to the controller/device, ensure that no
  694. * other commands are active on the controller/device
  695. */
  696. DPRINTK("@Softreset, CQ = 0x%x, CA = 0x%x, CC = 0x%x\n",
  697. ioread32(CQ + hcr_base),
  698. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  699. iowrite32(0xFFFF, CC + hcr_base);
  700. iowrite32(1, CQ + hcr_base);
  701. temp = ata_wait_register(CQ + hcr_base, 0x1, 0x1, 1, 5000);
  702. if (temp & 0x1) {
  703. ata_port_printk(ap, KERN_WARNING, "ATA_SRST issue failed\n");
  704. DPRINTK("Softreset@5000,CQ=0x%x,CA=0x%x,CC=0x%x\n",
  705. ioread32(CQ + hcr_base),
  706. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  707. sata_fsl_scr_read(ap, SCR_ERROR, &Serror);
  708. DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  709. DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  710. DPRINTK("Serror = 0x%x\n", Serror);
  711. goto err;
  712. }
  713. msleep(1);
  714. /*
  715. * SATA device enters reset state after receving a Control register
  716. * FIS with SRST bit asserted and it awaits another H2D Control reg.
  717. * FIS with SRST bit cleared, then the device does internal diags &
  718. * initialization, followed by indicating it's initialization status
  719. * using ATA signature D2H register FIS to the host controller.
  720. */
  721. sata_fsl_setup_cmd_hdr_entry(pp, 0, CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
  722. tf.ctl &= ~ATA_SRST; /* 2nd H2D Ctl. register FIS */
  723. ata_tf_to_fis(&tf, 0, 0, cfis);
  724. iowrite32(1, CQ + hcr_base);
  725. msleep(150); /* ?? */
  726. /*
  727. * The above command would have signalled an interrupt on command
  728. * complete, which needs special handling, by clearing the Nth
  729. * command bit of the CCreg
  730. */
  731. iowrite32(0x01, CC + hcr_base); /* We know it will be cmd#0 always */
  732. DPRINTK("SATA FSL : Now checking device signature\n");
  733. *class = ATA_DEV_NONE;
  734. /* Verify if SStatus indicates device presence */
  735. if (ata_link_online(link)) {
  736. /*
  737. * if we are here, device presence has been detected,
  738. * 1st D2H FIS would have been received, but sfis in
  739. * command desc. is not updated, but signature register
  740. * would have been updated
  741. */
  742. *class = sata_fsl_dev_classify(ap);
  743. DPRINTK("class = %d\n", *class);
  744. VPRINTK("ccreg = 0x%x\n", ioread32(hcr_base + CC));
  745. VPRINTK("cereg = 0x%x\n", ioread32(hcr_base + CE));
  746. }
  747. return 0;
  748. err:
  749. return -EIO;
  750. }
  751. static void sata_fsl_post_internal_cmd(struct ata_queued_cmd *qc)
  752. {
  753. if (qc->flags & ATA_QCFLAG_FAILED)
  754. qc->err_mask |= AC_ERR_OTHER;
  755. if (qc->err_mask) {
  756. /* make DMA engine forget about the failed command */
  757. }
  758. }
  759. static void sata_fsl_error_intr(struct ata_port *ap)
  760. {
  761. struct ata_link *link = &ap->link;
  762. struct ata_eh_info *ehi = &link->eh_info;
  763. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  764. void __iomem *hcr_base = host_priv->hcr_base;
  765. u32 hstatus, dereg, cereg = 0, SError = 0;
  766. unsigned int err_mask = 0, action = 0;
  767. struct ata_queued_cmd *qc;
  768. int freeze = 0;
  769. hstatus = ioread32(hcr_base + HSTATUS);
  770. cereg = ioread32(hcr_base + CE);
  771. ata_ehi_clear_desc(ehi);
  772. /*
  773. * Handle & Clear SError
  774. */
  775. sata_fsl_scr_read(ap, SCR_ERROR, &SError);
  776. if (unlikely(SError & 0xFFFF0000)) {
  777. sata_fsl_scr_write(ap, SCR_ERROR, SError);
  778. err_mask |= AC_ERR_ATA_BUS;
  779. }
  780. DPRINTK("error_intr,hStat=0x%x,CE=0x%x,DE =0x%x,SErr=0x%x\n",
  781. hstatus, cereg, ioread32(hcr_base + DE), SError);
  782. /* handle single device errors */
  783. if (cereg) {
  784. /*
  785. * clear the command error, also clears queue to the device
  786. * in error, and we can (re)issue commands to this device.
  787. * When a device is in error all commands queued into the
  788. * host controller and at the device are considered aborted
  789. * and the queue for that device is stopped. Now, after
  790. * clearing the device error, we can issue commands to the
  791. * device to interrogate it to find the source of the error.
  792. */
  793. dereg = ioread32(hcr_base + DE);
  794. iowrite32(dereg, hcr_base + DE);
  795. iowrite32(cereg, hcr_base + CE);
  796. DPRINTK("single device error, CE=0x%x, DE=0x%x\n",
  797. ioread32(hcr_base + CE), ioread32(hcr_base + DE));
  798. /*
  799. * We should consider this as non fatal error, and TF must
  800. * be updated as done below.
  801. */
  802. err_mask |= AC_ERR_DEV;
  803. }
  804. /* handle fatal errors */
  805. if (hstatus & FATAL_ERROR_DECODE) {
  806. err_mask |= AC_ERR_ATA_BUS;
  807. action |= ATA_EH_RESET;
  808. /* how will fatal error interrupts be completed ?? */
  809. freeze = 1;
  810. }
  811. /* Handle PHYRDY change notification */
  812. if (hstatus & INT_ON_PHYRDY_CHG) {
  813. DPRINTK("SATA FSL: PHYRDY change indication\n");
  814. /* Setup a soft-reset EH action */
  815. ata_ehi_hotplugged(ehi);
  816. freeze = 1;
  817. }
  818. /* record error info */
  819. qc = ata_qc_from_tag(ap, link->active_tag);
  820. if (qc) {
  821. sata_fsl_cache_taskfile_from_d2h_fis(qc, qc->ap);
  822. qc->err_mask |= err_mask;
  823. } else
  824. ehi->err_mask |= err_mask;
  825. ehi->action |= action;
  826. ehi->serror |= SError;
  827. /* freeze or abort */
  828. if (freeze)
  829. ata_port_freeze(ap);
  830. else
  831. ata_port_abort(ap);
  832. }
  833. static void sata_fsl_qc_complete(struct ata_queued_cmd *qc)
  834. {
  835. if (qc->flags & ATA_QCFLAG_RESULT_TF) {
  836. DPRINTK("xx_qc_complete called\n");
  837. sata_fsl_cache_taskfile_from_d2h_fis(qc, qc->ap);
  838. }
  839. }
  840. static void sata_fsl_host_intr(struct ata_port *ap)
  841. {
  842. struct ata_link *link = &ap->link;
  843. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  844. void __iomem *hcr_base = host_priv->hcr_base;
  845. u32 hstatus, qc_active = 0;
  846. struct ata_queued_cmd *qc;
  847. u32 SError;
  848. hstatus = ioread32(hcr_base + HSTATUS);
  849. sata_fsl_scr_read(ap, SCR_ERROR, &SError);
  850. if (unlikely(SError & 0xFFFF0000)) {
  851. DPRINTK("serror @host_intr : 0x%x\n", SError);
  852. sata_fsl_error_intr(ap);
  853. }
  854. if (unlikely(hstatus & INT_ON_ERROR)) {
  855. DPRINTK("error interrupt!!\n");
  856. sata_fsl_error_intr(ap);
  857. return;
  858. }
  859. if (link->sactive) { /* only true for NCQ commands */
  860. int i;
  861. /* Read command completed register */
  862. qc_active = ioread32(hcr_base + CC);
  863. /* clear CC bit, this will also complete the interrupt */
  864. iowrite32(qc_active, hcr_base + CC);
  865. DPRINTK("Status of all queues :\n");
  866. DPRINTK("qc_active/CC = 0x%x, CA = 0x%x, CE=0x%x\n",
  867. qc_active, ioread32(hcr_base + CA),
  868. ioread32(hcr_base + CE));
  869. for (i = 0; i < SATA_FSL_QUEUE_DEPTH; i++) {
  870. if (qc_active & (1 << i)) {
  871. qc = ata_qc_from_tag(ap, i);
  872. if (qc) {
  873. sata_fsl_qc_complete(qc);
  874. ata_qc_complete(qc);
  875. }
  876. DPRINTK
  877. ("completing ncq cmd,tag=%d,CC=0x%x,CA=0x%x\n",
  878. i, ioread32(hcr_base + CC),
  879. ioread32(hcr_base + CA));
  880. }
  881. }
  882. return;
  883. } else if (ap->qc_active) {
  884. iowrite32(1, hcr_base + CC);
  885. qc = ata_qc_from_tag(ap, link->active_tag);
  886. DPRINTK("completing non-ncq cmd, tag=%d,CC=0x%x\n",
  887. link->active_tag, ioread32(hcr_base + CC));
  888. if (qc) {
  889. sata_fsl_qc_complete(qc);
  890. ata_qc_complete(qc);
  891. }
  892. } else {
  893. /* Spurious Interrupt!! */
  894. DPRINTK("spurious interrupt!!, CC = 0x%x\n",
  895. ioread32(hcr_base + CC));
  896. return;
  897. }
  898. }
  899. static irqreturn_t sata_fsl_interrupt(int irq, void *dev_instance)
  900. {
  901. struct ata_host *host = dev_instance;
  902. struct sata_fsl_host_priv *host_priv = host->private_data;
  903. void __iomem *hcr_base = host_priv->hcr_base;
  904. u32 interrupt_enables;
  905. unsigned handled = 0;
  906. struct ata_port *ap;
  907. /* ack. any pending IRQs for this controller/port */
  908. interrupt_enables = ioread32(hcr_base + HSTATUS);
  909. interrupt_enables &= 0x3F;
  910. DPRINTK("interrupt status 0x%x\n", interrupt_enables);
  911. if (!interrupt_enables)
  912. return IRQ_NONE;
  913. spin_lock(&host->lock);
  914. /* Assuming one port per host controller */
  915. ap = host->ports[0];
  916. if (ap) {
  917. sata_fsl_host_intr(ap);
  918. } else {
  919. dev_printk(KERN_WARNING, host->dev,
  920. "interrupt on disabled port 0\n");
  921. }
  922. iowrite32(interrupt_enables, hcr_base + HSTATUS);
  923. handled = 1;
  924. spin_unlock(&host->lock);
  925. return IRQ_RETVAL(handled);
  926. }
  927. /*
  928. * Multiple ports are represented by multiple SATA controllers with
  929. * one port per controller
  930. */
  931. static int sata_fsl_init_controller(struct ata_host *host)
  932. {
  933. struct sata_fsl_host_priv *host_priv = host->private_data;
  934. void __iomem *hcr_base = host_priv->hcr_base;
  935. u32 temp;
  936. /*
  937. * NOTE : We cannot bring the controller online before setting
  938. * the CHBA, hence main controller initialization is done as
  939. * part of the port_start() callback
  940. */
  941. /* ack. any pending IRQs for this controller/port */
  942. temp = ioread32(hcr_base + HSTATUS);
  943. if (temp & 0x3F)
  944. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  945. /* Keep interrupts disabled on the controller */
  946. temp = ioread32(hcr_base + HCONTROL);
  947. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  948. /* Disable interrupt coalescing control(icc), for the moment */
  949. DPRINTK("icc = 0x%x\n", ioread32(hcr_base + ICC));
  950. iowrite32(0x01000000, hcr_base + ICC);
  951. /* clear error registers, SError is cleared by libATA */
  952. iowrite32(0x00000FFFF, hcr_base + CE);
  953. iowrite32(0x00000FFFF, hcr_base + DE);
  954. /* initially assuming no Port multiplier, set CQPMP to 0 */
  955. iowrite32(0x0, hcr_base + CQPMP);
  956. /*
  957. * host controller will be brought on-line, during xx_port_start()
  958. * callback, that should also initiate the OOB, COMINIT sequence
  959. */
  960. DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  961. DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  962. return 0;
  963. }
  964. /*
  965. * scsi mid-layer and libata interface structures
  966. */
  967. static struct scsi_host_template sata_fsl_sht = {
  968. ATA_NCQ_SHT("sata_fsl"),
  969. .can_queue = SATA_FSL_QUEUE_DEPTH,
  970. .sg_tablesize = SATA_FSL_MAX_PRD_USABLE,
  971. .dma_boundary = ATA_DMA_BOUNDARY,
  972. };
  973. static const struct ata_port_operations sata_fsl_ops = {
  974. .inherits = &sata_port_ops,
  975. .sff_check_status = sata_fsl_check_status,
  976. .sff_check_altstatus = sata_fsl_check_status,
  977. .qc_prep = sata_fsl_qc_prep,
  978. .qc_issue = sata_fsl_qc_issue,
  979. .qc_fill_rtf = sata_fsl_qc_fill_rtf,
  980. .scr_read = sata_fsl_scr_read,
  981. .scr_write = sata_fsl_scr_write,
  982. .freeze = sata_fsl_freeze,
  983. .thaw = sata_fsl_thaw,
  984. .softreset = sata_fsl_softreset,
  985. .post_internal_cmd = sata_fsl_post_internal_cmd,
  986. .port_start = sata_fsl_port_start,
  987. .port_stop = sata_fsl_port_stop,
  988. };
  989. static const struct ata_port_info sata_fsl_port_info[] = {
  990. {
  991. .flags = SATA_FSL_HOST_FLAGS,
  992. .pio_mask = 0x1f, /* pio 0-4 */
  993. .udma_mask = 0x7f, /* udma 0-6 */
  994. .port_ops = &sata_fsl_ops,
  995. },
  996. };
  997. static int sata_fsl_probe(struct of_device *ofdev,
  998. const struct of_device_id *match)
  999. {
  1000. int retval = 0;
  1001. void __iomem *hcr_base = NULL;
  1002. void __iomem *ssr_base = NULL;
  1003. void __iomem *csr_base = NULL;
  1004. struct sata_fsl_host_priv *host_priv = NULL;
  1005. int irq;
  1006. struct ata_host *host;
  1007. struct ata_port_info pi = sata_fsl_port_info[0];
  1008. const struct ata_port_info *ppi[] = { &pi, NULL };
  1009. dev_printk(KERN_INFO, &ofdev->dev,
  1010. "Sata FSL Platform/CSB Driver init\n");
  1011. hcr_base = of_iomap(ofdev->node, 0);
  1012. if (!hcr_base)
  1013. goto error_exit_with_cleanup;
  1014. ssr_base = hcr_base + 0x100;
  1015. csr_base = hcr_base + 0x140;
  1016. DPRINTK("@reset i/o = 0x%x\n", ioread32(csr_base + TRANSCFG));
  1017. DPRINTK("sizeof(cmd_desc) = %d\n", sizeof(struct command_desc));
  1018. DPRINTK("sizeof(#define cmd_desc) = %d\n", SATA_FSL_CMD_DESC_SIZE);
  1019. host_priv = kzalloc(sizeof(struct sata_fsl_host_priv), GFP_KERNEL);
  1020. if (!host_priv)
  1021. goto error_exit_with_cleanup;
  1022. host_priv->hcr_base = hcr_base;
  1023. host_priv->ssr_base = ssr_base;
  1024. host_priv->csr_base = csr_base;
  1025. irq = irq_of_parse_and_map(ofdev->node, 0);
  1026. if (irq < 0) {
  1027. dev_printk(KERN_ERR, &ofdev->dev, "invalid irq from platform\n");
  1028. goto error_exit_with_cleanup;
  1029. }
  1030. host_priv->irq = irq;
  1031. /* allocate host structure */
  1032. host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_FSL_MAX_PORTS);
  1033. /* host->iomap is not used currently */
  1034. host->private_data = host_priv;
  1035. /* setup port(s) */
  1036. host->ports[0]->ioaddr.cmd_addr = host_priv->hcr_base;
  1037. host->ports[0]->ioaddr.scr_addr = host_priv->ssr_base;
  1038. /* initialize host controller */
  1039. sata_fsl_init_controller(host);
  1040. /*
  1041. * Now, register with libATA core, this will also initiate the
  1042. * device discovery process, invoking our port_start() handler &
  1043. * error_handler() to execute a dummy Softreset EH session
  1044. */
  1045. ata_host_activate(host, irq, sata_fsl_interrupt, SATA_FSL_IRQ_FLAG,
  1046. &sata_fsl_sht);
  1047. dev_set_drvdata(&ofdev->dev, host);
  1048. return 0;
  1049. error_exit_with_cleanup:
  1050. if (hcr_base)
  1051. iounmap(hcr_base);
  1052. if (host_priv)
  1053. kfree(host_priv);
  1054. return retval;
  1055. }
  1056. static int sata_fsl_remove(struct of_device *ofdev)
  1057. {
  1058. struct ata_host *host = dev_get_drvdata(&ofdev->dev);
  1059. struct sata_fsl_host_priv *host_priv = host->private_data;
  1060. ata_host_detach(host);
  1061. dev_set_drvdata(&ofdev->dev, NULL);
  1062. irq_dispose_mapping(host_priv->irq);
  1063. iounmap(host_priv->hcr_base);
  1064. kfree(host_priv);
  1065. return 0;
  1066. }
  1067. static struct of_device_id fsl_sata_match[] = {
  1068. {
  1069. .compatible = "fsl,pq-sata",
  1070. },
  1071. {},
  1072. };
  1073. MODULE_DEVICE_TABLE(of, fsl_sata_match);
  1074. static struct of_platform_driver fsl_sata_driver = {
  1075. .name = "fsl-sata",
  1076. .match_table = fsl_sata_match,
  1077. .probe = sata_fsl_probe,
  1078. .remove = sata_fsl_remove,
  1079. };
  1080. static int __init sata_fsl_init(void)
  1081. {
  1082. of_register_platform_driver(&fsl_sata_driver);
  1083. return 0;
  1084. }
  1085. static void __exit sata_fsl_exit(void)
  1086. {
  1087. of_unregister_platform_driver(&fsl_sata_driver);
  1088. }
  1089. MODULE_LICENSE("GPL");
  1090. MODULE_AUTHOR("Ashish Kalra, Freescale Semiconductor");
  1091. MODULE_DESCRIPTION("Freescale 3.0Gbps SATA controller low level driver");
  1092. MODULE_VERSION("1.10");
  1093. module_init(sata_fsl_init);
  1094. module_exit(sata_fsl_exit);