sata_fsl.c 43 KB

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