sata_fsl.c 42 KB

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