sata_fsl.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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 int sata_fsl_scr_write(struct ata_port *ap, unsigned int sc_reg_in,
  391. u32 val)
  392. {
  393. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  394. void __iomem *ssr_base = host_priv->ssr_base;
  395. unsigned int sc_reg;
  396. switch (sc_reg_in) {
  397. case SCR_STATUS:
  398. case SCR_ERROR:
  399. case SCR_CONTROL:
  400. case SCR_ACTIVE:
  401. sc_reg = sc_reg_in;
  402. break;
  403. default:
  404. return -EINVAL;
  405. }
  406. VPRINTK("xx_scr_write, reg_in = %d\n", sc_reg);
  407. iowrite32(val, ssr_base + (sc_reg * 4));
  408. return 0;
  409. }
  410. static int sata_fsl_scr_read(struct ata_port *ap, unsigned int sc_reg_in,
  411. u32 *val)
  412. {
  413. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  414. void __iomem *ssr_base = host_priv->ssr_base;
  415. unsigned int sc_reg;
  416. switch (sc_reg_in) {
  417. case SCR_STATUS:
  418. case SCR_ERROR:
  419. case SCR_CONTROL:
  420. case SCR_ACTIVE:
  421. sc_reg = sc_reg_in;
  422. break;
  423. default:
  424. return -EINVAL;
  425. }
  426. VPRINTK("xx_scr_read, reg_in = %d\n", sc_reg);
  427. *val = ioread32(ssr_base + (sc_reg * 4));
  428. return 0;
  429. }
  430. static void sata_fsl_freeze(struct ata_port *ap)
  431. {
  432. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  433. void __iomem *hcr_base = host_priv->hcr_base;
  434. u32 temp;
  435. VPRINTK("xx_freeze, CQ=0x%x, CA=0x%x, CE=0x%x, DE=0x%x\n",
  436. ioread32(CQ + hcr_base),
  437. ioread32(CA + hcr_base),
  438. ioread32(CE + hcr_base), ioread32(DE + hcr_base));
  439. VPRINTK("CmdStat = 0x%x\n",
  440. ioread32(host_priv->csr_base + COMMANDSTAT));
  441. /* disable interrupts on the controller/port */
  442. temp = ioread32(hcr_base + HCONTROL);
  443. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  444. VPRINTK("in xx_freeze : HControl = 0x%x, HStatus = 0x%x\n",
  445. ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
  446. }
  447. static void sata_fsl_thaw(struct ata_port *ap)
  448. {
  449. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  450. void __iomem *hcr_base = host_priv->hcr_base;
  451. u32 temp;
  452. /* ack. any pending IRQs for this controller/port */
  453. temp = ioread32(hcr_base + HSTATUS);
  454. VPRINTK("xx_thaw, pending IRQs = 0x%x\n", (temp & 0x3F));
  455. if (temp & 0x3F)
  456. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  457. /* enable interrupts on the controller/port */
  458. temp = ioread32(hcr_base + HCONTROL);
  459. iowrite32((temp | DEFAULT_PORT_IRQ_ENABLE_MASK), hcr_base + HCONTROL);
  460. VPRINTK("xx_thaw : HControl = 0x%x, HStatus = 0x%x\n",
  461. ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
  462. }
  463. /*
  464. * NOTE : 1st D2H FIS from device does not update sfis in command descriptor.
  465. */
  466. static inline void sata_fsl_cache_taskfile_from_d2h_fis(struct ata_queued_cmd
  467. *qc,
  468. struct ata_port *ap)
  469. {
  470. struct sata_fsl_port_priv *pp = ap->private_data;
  471. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  472. void __iomem *hcr_base = host_priv->hcr_base;
  473. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  474. struct command_desc *cd;
  475. cd = pp->cmdentry + tag;
  476. ata_tf_from_fis(cd->sfis, &pp->tf);
  477. }
  478. static u8 sata_fsl_check_status(struct ata_port *ap)
  479. {
  480. struct sata_fsl_port_priv *pp = ap->private_data;
  481. return pp->tf.command;
  482. }
  483. static void sata_fsl_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  484. {
  485. struct sata_fsl_port_priv *pp = ap->private_data;
  486. *tf = pp->tf;
  487. }
  488. static int sata_fsl_port_start(struct ata_port *ap)
  489. {
  490. struct device *dev = ap->host->dev;
  491. struct sata_fsl_port_priv *pp;
  492. int retval;
  493. void *mem;
  494. dma_addr_t mem_dma;
  495. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  496. void __iomem *hcr_base = host_priv->hcr_base;
  497. u32 temp;
  498. pp = kzalloc(sizeof(*pp), GFP_KERNEL);
  499. if (!pp)
  500. return -ENOMEM;
  501. mem = dma_alloc_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ, &mem_dma,
  502. GFP_KERNEL);
  503. if (!mem) {
  504. kfree(pp);
  505. return -ENOMEM;
  506. }
  507. memset(mem, 0, SATA_FSL_PORT_PRIV_DMA_SZ);
  508. pp->cmdslot = mem;
  509. pp->cmdslot_paddr = mem_dma;
  510. mem += SATA_FSL_CMD_SLOT_SIZE;
  511. mem_dma += SATA_FSL_CMD_SLOT_SIZE;
  512. pp->cmdentry = mem;
  513. pp->cmdentry_paddr = mem_dma;
  514. ap->private_data = pp;
  515. VPRINTK("CHBA = 0x%x, cmdentry_phys = 0x%x\n",
  516. pp->cmdslot_paddr, pp->cmdentry_paddr);
  517. /* Now, update the CHBA register in host controller cmd register set */
  518. iowrite32(pp->cmdslot_paddr & 0xffffffff, hcr_base + CHBA);
  519. /*
  520. * Now, we can bring the controller on-line & also initiate
  521. * the COMINIT sequence, we simply return here and the boot-probing
  522. * & device discovery process is re-initiated by libATA using a
  523. * Softreset EH (dummy) session. Hence, boot probing and device
  524. * discovey will be part of sata_fsl_softreset() callback.
  525. */
  526. temp = ioread32(hcr_base + HCONTROL);
  527. iowrite32((temp | HCONTROL_ONLINE_PHY_RST), hcr_base + HCONTROL);
  528. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  529. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  530. VPRINTK("CHBA = 0x%x\n", ioread32(hcr_base + CHBA));
  531. #ifdef CONFIG_MPC8315_DS
  532. /*
  533. * Workaround for 8315DS board 3gbps link-up issue,
  534. * currently limit SATA port to GEN1 speed
  535. */
  536. sata_fsl_scr_read(ap, SCR_CONTROL, &temp);
  537. temp &= ~(0xF << 4);
  538. temp |= (0x1 << 4);
  539. sata_fsl_scr_write(ap, SCR_CONTROL, temp);
  540. sata_fsl_scr_read(ap, SCR_CONTROL, &temp);
  541. dev_printk(KERN_WARNING, dev, "scr_control, speed limited to %x\n",
  542. temp);
  543. #endif
  544. return 0;
  545. }
  546. static void sata_fsl_port_stop(struct ata_port *ap)
  547. {
  548. struct device *dev = ap->host->dev;
  549. struct sata_fsl_port_priv *pp = ap->private_data;
  550. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  551. void __iomem *hcr_base = host_priv->hcr_base;
  552. u32 temp;
  553. /*
  554. * Force host controller to go off-line, aborting current operations
  555. */
  556. temp = ioread32(hcr_base + HCONTROL);
  557. temp &= ~HCONTROL_ONLINE_PHY_RST;
  558. temp |= HCONTROL_FORCE_OFFLINE;
  559. iowrite32(temp, hcr_base + HCONTROL);
  560. /* Poll for controller to go offline - should happen immediately */
  561. ata_wait_register(hcr_base + HSTATUS, ONLINE, ONLINE, 1, 1);
  562. ap->private_data = NULL;
  563. dma_free_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ,
  564. pp->cmdslot, pp->cmdslot_paddr);
  565. kfree(pp);
  566. }
  567. static unsigned int sata_fsl_dev_classify(struct ata_port *ap)
  568. {
  569. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  570. void __iomem *hcr_base = host_priv->hcr_base;
  571. struct ata_taskfile tf;
  572. u32 temp;
  573. temp = ioread32(hcr_base + SIGNATURE);
  574. VPRINTK("raw sig = 0x%x\n", temp);
  575. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  576. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  577. tf.lbah = (temp >> 24) & 0xff;
  578. tf.lbam = (temp >> 16) & 0xff;
  579. tf.lbal = (temp >> 8) & 0xff;
  580. tf.nsect = temp & 0xff;
  581. return ata_dev_classify(&tf);
  582. }
  583. static int sata_fsl_softreset(struct ata_link *link, unsigned int *class,
  584. unsigned long deadline)
  585. {
  586. struct ata_port *ap = link->ap;
  587. struct sata_fsl_port_priv *pp = ap->private_data;
  588. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  589. void __iomem *hcr_base = host_priv->hcr_base;
  590. u32 temp;
  591. struct ata_taskfile tf;
  592. u8 *cfis;
  593. u32 Serror;
  594. int i = 0;
  595. unsigned long start_jiffies;
  596. DPRINTK("in xx_softreset\n");
  597. try_offline_again:
  598. /*
  599. * Force host controller to go off-line, aborting current operations
  600. */
  601. temp = ioread32(hcr_base + HCONTROL);
  602. temp &= ~HCONTROL_ONLINE_PHY_RST;
  603. iowrite32(temp, hcr_base + HCONTROL);
  604. /* Poll for controller to go offline */
  605. temp = ata_wait_register(hcr_base + HSTATUS, ONLINE, ONLINE, 1, 500);
  606. if (temp & ONLINE) {
  607. ata_port_printk(ap, KERN_ERR,
  608. "Softreset failed, not off-lined %d\n", i);
  609. /*
  610. * Try to offline controller atleast twice
  611. */
  612. i++;
  613. if (i == 2)
  614. goto err;
  615. else
  616. goto try_offline_again;
  617. }
  618. DPRINTK("softreset, controller off-lined\n");
  619. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  620. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  621. /*
  622. * PHY reset should remain asserted for atleast 1ms
  623. */
  624. msleep(1);
  625. /*
  626. * Now, bring the host controller online again, this can take time
  627. * as PHY reset and communication establishment, 1st D2H FIS and
  628. * device signature update is done, on safe side assume 500ms
  629. * NOTE : Host online status may be indicated immediately!!
  630. */
  631. temp = ioread32(hcr_base + HCONTROL);
  632. temp |= (HCONTROL_ONLINE_PHY_RST | HCONTROL_SNOOP_ENABLE);
  633. iowrite32(temp, hcr_base + HCONTROL);
  634. temp = ata_wait_register(hcr_base + HSTATUS, ONLINE, 0, 1, 500);
  635. if (!(temp & ONLINE)) {
  636. ata_port_printk(ap, KERN_ERR,
  637. "Softreset failed, not on-lined\n");
  638. goto err;
  639. }
  640. DPRINTK("softreset, controller off-lined & on-lined\n");
  641. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  642. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  643. /*
  644. * First, wait for the PHYRDY change to occur before waiting for
  645. * the signature, and also verify if SStatus indicates device
  646. * presence
  647. */
  648. temp = ata_wait_register(hcr_base + HSTATUS, 0xFF, 0, 1, 500);
  649. if ((!(temp & 0x10)) || ata_link_offline(link)) {
  650. ata_port_printk(ap, KERN_WARNING,
  651. "No Device OR PHYRDY change,Hstatus = 0x%x\n",
  652. ioread32(hcr_base + HSTATUS));
  653. goto err;
  654. }
  655. /*
  656. * Wait for the first D2H from device,i.e,signature update notification
  657. */
  658. start_jiffies = jiffies;
  659. temp = ata_wait_register(hcr_base + HSTATUS, 0xFF, 0x10,
  660. 500, jiffies_to_msecs(deadline - start_jiffies));
  661. if ((temp & 0xFF) != 0x18) {
  662. ata_port_printk(ap, KERN_WARNING, "No Signature Update\n");
  663. goto err;
  664. } else {
  665. ata_port_printk(ap, KERN_INFO,
  666. "Signature Update detected @ %d msecs\n",
  667. jiffies_to_msecs(jiffies - start_jiffies));
  668. }
  669. /*
  670. * Send a device reset (SRST) explicitly on command slot #0
  671. * Check : will the command queue (reg) be cleared during offlining ??
  672. * Also we will be online only if Phy commn. has been established
  673. * and device presence has been detected, therefore if we have
  674. * reached here, we can send a command to the target device
  675. */
  676. DPRINTK("Sending SRST/device reset\n");
  677. ata_tf_init(link->device, &tf);
  678. cfis = (u8 *) &pp->cmdentry->cfis;
  679. /* device reset/SRST is a control register update FIS, uses tag0 */
  680. sata_fsl_setup_cmd_hdr_entry(pp, 0,
  681. SRST_CMD | CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
  682. tf.ctl |= ATA_SRST; /* setup SRST bit in taskfile control reg */
  683. ata_tf_to_fis(&tf, 0, 0, cfis);
  684. DPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x, 0x%x\n",
  685. cfis[0], cfis[1], cfis[2], cfis[3]);
  686. /*
  687. * Queue SRST command to the controller/device, ensure that no
  688. * other commands are active on the controller/device
  689. */
  690. DPRINTK("@Softreset, CQ = 0x%x, CA = 0x%x, CC = 0x%x\n",
  691. ioread32(CQ + hcr_base),
  692. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  693. iowrite32(0xFFFF, CC + hcr_base);
  694. iowrite32(1, CQ + hcr_base);
  695. temp = ata_wait_register(CQ + hcr_base, 0x1, 0x1, 1, 5000);
  696. if (temp & 0x1) {
  697. ata_port_printk(ap, KERN_WARNING, "ATA_SRST issue failed\n");
  698. DPRINTK("Softreset@5000,CQ=0x%x,CA=0x%x,CC=0x%x\n",
  699. ioread32(CQ + hcr_base),
  700. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  701. sata_fsl_scr_read(ap, SCR_ERROR, &Serror);
  702. DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  703. DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  704. DPRINTK("Serror = 0x%x\n", Serror);
  705. goto err;
  706. }
  707. msleep(1);
  708. /*
  709. * SATA device enters reset state after receving a Control register
  710. * FIS with SRST bit asserted and it awaits another H2D Control reg.
  711. * FIS with SRST bit cleared, then the device does internal diags &
  712. * initialization, followed by indicating it's initialization status
  713. * using ATA signature D2H register FIS to the host controller.
  714. */
  715. sata_fsl_setup_cmd_hdr_entry(pp, 0, CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
  716. tf.ctl &= ~ATA_SRST; /* 2nd H2D Ctl. register FIS */
  717. ata_tf_to_fis(&tf, 0, 0, cfis);
  718. iowrite32(1, CQ + hcr_base);
  719. msleep(150); /* ?? */
  720. /*
  721. * The above command would have signalled an interrupt on command
  722. * complete, which needs special handling, by clearing the Nth
  723. * command bit of the CCreg
  724. */
  725. iowrite32(0x01, CC + hcr_base); /* We know it will be cmd#0 always */
  726. DPRINTK("SATA FSL : Now checking device signature\n");
  727. *class = ATA_DEV_NONE;
  728. /* Verify if SStatus indicates device presence */
  729. if (ata_link_online(link)) {
  730. /*
  731. * if we are here, device presence has been detected,
  732. * 1st D2H FIS would have been received, but sfis in
  733. * command desc. is not updated, but signature register
  734. * would have been updated
  735. */
  736. *class = sata_fsl_dev_classify(ap);
  737. DPRINTK("class = %d\n", *class);
  738. VPRINTK("ccreg = 0x%x\n", ioread32(hcr_base + CC));
  739. VPRINTK("cereg = 0x%x\n", ioread32(hcr_base + CE));
  740. }
  741. return 0;
  742. err:
  743. return -EIO;
  744. }
  745. static void sata_fsl_post_internal_cmd(struct ata_queued_cmd *qc)
  746. {
  747. if (qc->flags & ATA_QCFLAG_FAILED)
  748. qc->err_mask |= AC_ERR_OTHER;
  749. if (qc->err_mask) {
  750. /* make DMA engine forget about the failed command */
  751. }
  752. }
  753. static void sata_fsl_error_intr(struct ata_port *ap)
  754. {
  755. struct ata_link *link = &ap->link;
  756. struct ata_eh_info *ehi = &link->eh_info;
  757. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  758. void __iomem *hcr_base = host_priv->hcr_base;
  759. u32 hstatus, dereg, cereg = 0, SError = 0;
  760. unsigned int err_mask = 0, action = 0;
  761. struct ata_queued_cmd *qc;
  762. int freeze = 0;
  763. hstatus = ioread32(hcr_base + HSTATUS);
  764. cereg = ioread32(hcr_base + CE);
  765. ata_ehi_clear_desc(ehi);
  766. /*
  767. * Handle & Clear SError
  768. */
  769. sata_fsl_scr_read(ap, SCR_ERROR, &SError);
  770. if (unlikely(SError & 0xFFFF0000)) {
  771. sata_fsl_scr_write(ap, SCR_ERROR, SError);
  772. err_mask |= AC_ERR_ATA_BUS;
  773. }
  774. DPRINTK("error_intr,hStat=0x%x,CE=0x%x,DE =0x%x,SErr=0x%x\n",
  775. hstatus, cereg, ioread32(hcr_base + DE), SError);
  776. /* handle single device errors */
  777. if (cereg) {
  778. /*
  779. * clear the command error, also clears queue to the device
  780. * in error, and we can (re)issue commands to this device.
  781. * When a device is in error all commands queued into the
  782. * host controller and at the device are considered aborted
  783. * and the queue for that device is stopped. Now, after
  784. * clearing the device error, we can issue commands to the
  785. * device to interrogate it to find the source of the error.
  786. */
  787. dereg = ioread32(hcr_base + DE);
  788. iowrite32(dereg, hcr_base + DE);
  789. iowrite32(cereg, hcr_base + CE);
  790. DPRINTK("single device error, CE=0x%x, DE=0x%x\n",
  791. ioread32(hcr_base + CE), ioread32(hcr_base + DE));
  792. /*
  793. * We should consider this as non fatal error, and TF must
  794. * be updated as done below.
  795. */
  796. err_mask |= AC_ERR_DEV;
  797. }
  798. /* handle fatal errors */
  799. if (hstatus & FATAL_ERROR_DECODE) {
  800. err_mask |= AC_ERR_ATA_BUS;
  801. action |= ATA_EH_RESET;
  802. /* how will fatal error interrupts be completed ?? */
  803. freeze = 1;
  804. }
  805. /* Handle PHYRDY change notification */
  806. if (hstatus & INT_ON_PHYRDY_CHG) {
  807. DPRINTK("SATA FSL: PHYRDY change indication\n");
  808. /* Setup a soft-reset EH action */
  809. ata_ehi_hotplugged(ehi);
  810. freeze = 1;
  811. }
  812. /* record error info */
  813. qc = ata_qc_from_tag(ap, link->active_tag);
  814. if (qc) {
  815. sata_fsl_cache_taskfile_from_d2h_fis(qc, qc->ap);
  816. qc->err_mask |= err_mask;
  817. } else
  818. ehi->err_mask |= err_mask;
  819. ehi->action |= action;
  820. ehi->serror |= SError;
  821. /* freeze or abort */
  822. if (freeze)
  823. ata_port_freeze(ap);
  824. else
  825. ata_port_abort(ap);
  826. }
  827. static void sata_fsl_qc_complete(struct ata_queued_cmd *qc)
  828. {
  829. if (qc->flags & ATA_QCFLAG_RESULT_TF) {
  830. DPRINTK("xx_qc_complete called\n");
  831. sata_fsl_cache_taskfile_from_d2h_fis(qc, qc->ap);
  832. }
  833. }
  834. static void sata_fsl_host_intr(struct ata_port *ap)
  835. {
  836. struct ata_link *link = &ap->link;
  837. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  838. void __iomem *hcr_base = host_priv->hcr_base;
  839. u32 hstatus, qc_active = 0;
  840. struct ata_queued_cmd *qc;
  841. u32 SError;
  842. hstatus = ioread32(hcr_base + HSTATUS);
  843. sata_fsl_scr_read(ap, SCR_ERROR, &SError);
  844. if (unlikely(SError & 0xFFFF0000)) {
  845. DPRINTK("serror @host_intr : 0x%x\n", SError);
  846. sata_fsl_error_intr(ap);
  847. }
  848. if (unlikely(hstatus & INT_ON_ERROR)) {
  849. DPRINTK("error interrupt!!\n");
  850. sata_fsl_error_intr(ap);
  851. return;
  852. }
  853. if (link->sactive) { /* only true for NCQ commands */
  854. int i;
  855. /* Read command completed register */
  856. qc_active = ioread32(hcr_base + CC);
  857. /* clear CC bit, this will also complete the interrupt */
  858. iowrite32(qc_active, hcr_base + CC);
  859. DPRINTK("Status of all queues :\n");
  860. DPRINTK("qc_active/CC = 0x%x, CA = 0x%x, CE=0x%x\n",
  861. qc_active, ioread32(hcr_base + CA),
  862. ioread32(hcr_base + CE));
  863. for (i = 0; i < SATA_FSL_QUEUE_DEPTH; i++) {
  864. if (qc_active & (1 << i)) {
  865. qc = ata_qc_from_tag(ap, i);
  866. if (qc) {
  867. sata_fsl_qc_complete(qc);
  868. ata_qc_complete(qc);
  869. }
  870. DPRINTK
  871. ("completing ncq cmd,tag=%d,CC=0x%x,CA=0x%x\n",
  872. i, ioread32(hcr_base + CC),
  873. ioread32(hcr_base + CA));
  874. }
  875. }
  876. return;
  877. } else if (ap->qc_active) {
  878. iowrite32(1, hcr_base + CC);
  879. qc = ata_qc_from_tag(ap, link->active_tag);
  880. DPRINTK("completing non-ncq cmd, tag=%d,CC=0x%x\n",
  881. link->active_tag, ioread32(hcr_base + CC));
  882. if (qc) {
  883. sata_fsl_qc_complete(qc);
  884. ata_qc_complete(qc);
  885. }
  886. } else {
  887. /* Spurious Interrupt!! */
  888. DPRINTK("spurious interrupt!!, CC = 0x%x\n",
  889. ioread32(hcr_base + CC));
  890. return;
  891. }
  892. }
  893. static irqreturn_t sata_fsl_interrupt(int irq, void *dev_instance)
  894. {
  895. struct ata_host *host = dev_instance;
  896. struct sata_fsl_host_priv *host_priv = host->private_data;
  897. void __iomem *hcr_base = host_priv->hcr_base;
  898. u32 interrupt_enables;
  899. unsigned handled = 0;
  900. struct ata_port *ap;
  901. /* ack. any pending IRQs for this controller/port */
  902. interrupt_enables = ioread32(hcr_base + HSTATUS);
  903. interrupt_enables &= 0x3F;
  904. DPRINTK("interrupt status 0x%x\n", interrupt_enables);
  905. if (!interrupt_enables)
  906. return IRQ_NONE;
  907. spin_lock(&host->lock);
  908. /* Assuming one port per host controller */
  909. ap = host->ports[0];
  910. if (ap) {
  911. sata_fsl_host_intr(ap);
  912. } else {
  913. dev_printk(KERN_WARNING, host->dev,
  914. "interrupt on disabled port 0\n");
  915. }
  916. iowrite32(interrupt_enables, hcr_base + HSTATUS);
  917. handled = 1;
  918. spin_unlock(&host->lock);
  919. return IRQ_RETVAL(handled);
  920. }
  921. /*
  922. * Multiple ports are represented by multiple SATA controllers with
  923. * one port per controller
  924. */
  925. static int sata_fsl_init_controller(struct ata_host *host)
  926. {
  927. struct sata_fsl_host_priv *host_priv = host->private_data;
  928. void __iomem *hcr_base = host_priv->hcr_base;
  929. u32 temp;
  930. /*
  931. * NOTE : We cannot bring the controller online before setting
  932. * the CHBA, hence main controller initialization is done as
  933. * part of the port_start() callback
  934. */
  935. /* ack. any pending IRQs for this controller/port */
  936. temp = ioread32(hcr_base + HSTATUS);
  937. if (temp & 0x3F)
  938. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  939. /* Keep interrupts disabled on the controller */
  940. temp = ioread32(hcr_base + HCONTROL);
  941. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  942. /* Disable interrupt coalescing control(icc), for the moment */
  943. DPRINTK("icc = 0x%x\n", ioread32(hcr_base + ICC));
  944. iowrite32(0x01000000, hcr_base + ICC);
  945. /* clear error registers, SError is cleared by libATA */
  946. iowrite32(0x00000FFFF, hcr_base + CE);
  947. iowrite32(0x00000FFFF, hcr_base + DE);
  948. /* initially assuming no Port multiplier, set CQPMP to 0 */
  949. iowrite32(0x0, hcr_base + CQPMP);
  950. /*
  951. * host controller will be brought on-line, during xx_port_start()
  952. * callback, that should also initiate the OOB, COMINIT sequence
  953. */
  954. DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  955. DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  956. return 0;
  957. }
  958. /*
  959. * scsi mid-layer and libata interface structures
  960. */
  961. static struct scsi_host_template sata_fsl_sht = {
  962. ATA_NCQ_SHT("sata_fsl"),
  963. .can_queue = SATA_FSL_QUEUE_DEPTH,
  964. .sg_tablesize = SATA_FSL_MAX_PRD_USABLE,
  965. .dma_boundary = ATA_DMA_BOUNDARY,
  966. };
  967. static const struct ata_port_operations sata_fsl_ops = {
  968. .inherits = &sata_port_ops,
  969. .check_status = sata_fsl_check_status,
  970. .check_altstatus = sata_fsl_check_status,
  971. .tf_read = sata_fsl_tf_read,
  972. .qc_prep = sata_fsl_qc_prep,
  973. .qc_issue = sata_fsl_qc_issue,
  974. .scr_read = sata_fsl_scr_read,
  975. .scr_write = sata_fsl_scr_write,
  976. .freeze = sata_fsl_freeze,
  977. .thaw = sata_fsl_thaw,
  978. .softreset = sata_fsl_softreset,
  979. .post_internal_cmd = sata_fsl_post_internal_cmd,
  980. .port_start = sata_fsl_port_start,
  981. .port_stop = sata_fsl_port_stop,
  982. };
  983. static const struct ata_port_info sata_fsl_port_info[] = {
  984. {
  985. .flags = SATA_FSL_HOST_FLAGS,
  986. .pio_mask = 0x1f, /* pio 0-4 */
  987. .udma_mask = 0x7f, /* udma 0-6 */
  988. .port_ops = &sata_fsl_ops,
  989. },
  990. };
  991. static int sata_fsl_probe(struct of_device *ofdev,
  992. const struct of_device_id *match)
  993. {
  994. int retval = 0;
  995. void __iomem *hcr_base = NULL;
  996. void __iomem *ssr_base = NULL;
  997. void __iomem *csr_base = NULL;
  998. struct sata_fsl_host_priv *host_priv = NULL;
  999. int irq;
  1000. struct ata_host *host;
  1001. struct ata_port_info pi = sata_fsl_port_info[0];
  1002. const struct ata_port_info *ppi[] = { &pi, NULL };
  1003. dev_printk(KERN_INFO, &ofdev->dev,
  1004. "Sata FSL Platform/CSB Driver init\n");
  1005. hcr_base = of_iomap(ofdev->node, 0);
  1006. if (!hcr_base)
  1007. goto error_exit_with_cleanup;
  1008. ssr_base = hcr_base + 0x100;
  1009. csr_base = hcr_base + 0x140;
  1010. DPRINTK("@reset i/o = 0x%x\n", ioread32(csr_base + TRANSCFG));
  1011. DPRINTK("sizeof(cmd_desc) = %d\n", sizeof(struct command_desc));
  1012. DPRINTK("sizeof(#define cmd_desc) = %d\n", SATA_FSL_CMD_DESC_SIZE);
  1013. host_priv = kzalloc(sizeof(struct sata_fsl_host_priv), GFP_KERNEL);
  1014. if (!host_priv)
  1015. goto error_exit_with_cleanup;
  1016. host_priv->hcr_base = hcr_base;
  1017. host_priv->ssr_base = ssr_base;
  1018. host_priv->csr_base = csr_base;
  1019. irq = irq_of_parse_and_map(ofdev->node, 0);
  1020. if (irq < 0) {
  1021. dev_printk(KERN_ERR, &ofdev->dev, "invalid irq from platform\n");
  1022. goto error_exit_with_cleanup;
  1023. }
  1024. host_priv->irq = irq;
  1025. /* allocate host structure */
  1026. host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_FSL_MAX_PORTS);
  1027. /* host->iomap is not used currently */
  1028. host->private_data = host_priv;
  1029. /* setup port(s) */
  1030. host->ports[0]->ioaddr.cmd_addr = host_priv->hcr_base;
  1031. host->ports[0]->ioaddr.scr_addr = host_priv->ssr_base;
  1032. /* initialize host controller */
  1033. sata_fsl_init_controller(host);
  1034. /*
  1035. * Now, register with libATA core, this will also initiate the
  1036. * device discovery process, invoking our port_start() handler &
  1037. * error_handler() to execute a dummy Softreset EH session
  1038. */
  1039. ata_host_activate(host, irq, sata_fsl_interrupt, SATA_FSL_IRQ_FLAG,
  1040. &sata_fsl_sht);
  1041. dev_set_drvdata(&ofdev->dev, host);
  1042. return 0;
  1043. error_exit_with_cleanup:
  1044. if (hcr_base)
  1045. iounmap(hcr_base);
  1046. if (host_priv)
  1047. kfree(host_priv);
  1048. return retval;
  1049. }
  1050. static int sata_fsl_remove(struct of_device *ofdev)
  1051. {
  1052. struct ata_host *host = dev_get_drvdata(&ofdev->dev);
  1053. struct sata_fsl_host_priv *host_priv = host->private_data;
  1054. ata_host_detach(host);
  1055. dev_set_drvdata(&ofdev->dev, NULL);
  1056. irq_dispose_mapping(host_priv->irq);
  1057. iounmap(host_priv->hcr_base);
  1058. kfree(host_priv);
  1059. return 0;
  1060. }
  1061. static struct of_device_id fsl_sata_match[] = {
  1062. {
  1063. .compatible = "fsl,pq-sata",
  1064. },
  1065. {},
  1066. };
  1067. MODULE_DEVICE_TABLE(of, fsl_sata_match);
  1068. static struct of_platform_driver fsl_sata_driver = {
  1069. .name = "fsl-sata",
  1070. .match_table = fsl_sata_match,
  1071. .probe = sata_fsl_probe,
  1072. .remove = sata_fsl_remove,
  1073. };
  1074. static int __init sata_fsl_init(void)
  1075. {
  1076. of_register_platform_driver(&fsl_sata_driver);
  1077. return 0;
  1078. }
  1079. static void __exit sata_fsl_exit(void)
  1080. {
  1081. of_unregister_platform_driver(&fsl_sata_driver);
  1082. }
  1083. MODULE_LICENSE("GPL");
  1084. MODULE_AUTHOR("Ashish Kalra, Freescale Semiconductor");
  1085. MODULE_DESCRIPTION("Freescale 3.0Gbps SATA controller low level driver");
  1086. MODULE_VERSION("1.10");
  1087. module_init(sata_fsl_init);
  1088. module_exit(sata_fsl_exit);