sata_fsl.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  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. struct device_attribute rx_watermark;
  253. };
  254. static void fsl_sata_set_irq_coalescing(struct ata_host *host,
  255. unsigned int count, unsigned int ticks)
  256. {
  257. struct sata_fsl_host_priv *host_priv = host->private_data;
  258. void __iomem *hcr_base = host_priv->hcr_base;
  259. if (count > ICC_MAX_INT_COUNT_THRESHOLD)
  260. count = ICC_MAX_INT_COUNT_THRESHOLD;
  261. else if (count < ICC_MIN_INT_COUNT_THRESHOLD)
  262. count = ICC_MIN_INT_COUNT_THRESHOLD;
  263. if (ticks > ICC_MAX_INT_TICKS_THRESHOLD)
  264. ticks = ICC_MAX_INT_TICKS_THRESHOLD;
  265. else if ((ICC_MIN_INT_TICKS_THRESHOLD == ticks) &&
  266. (count > ICC_MIN_INT_COUNT_THRESHOLD))
  267. ticks = ICC_SAFE_INT_TICKS;
  268. spin_lock(&host->lock);
  269. iowrite32((count << 24 | ticks), hcr_base + ICC);
  270. intr_coalescing_count = count;
  271. intr_coalescing_ticks = ticks;
  272. spin_unlock(&host->lock);
  273. DPRINTK("interrupt coalescing, count = 0x%x, ticks = %x\n",
  274. intr_coalescing_count, intr_coalescing_ticks);
  275. DPRINTK("ICC register status: (hcr base: 0x%x) = 0x%x\n",
  276. hcr_base, ioread32(hcr_base + ICC));
  277. }
  278. static ssize_t fsl_sata_intr_coalescing_show(struct device *dev,
  279. struct device_attribute *attr, char *buf)
  280. {
  281. return sprintf(buf, "%d %d\n",
  282. intr_coalescing_count, intr_coalescing_ticks);
  283. }
  284. static ssize_t fsl_sata_intr_coalescing_store(struct device *dev,
  285. struct device_attribute *attr,
  286. const char *buf, size_t count)
  287. {
  288. unsigned int coalescing_count, coalescing_ticks;
  289. if (sscanf(buf, "%d%d",
  290. &coalescing_count,
  291. &coalescing_ticks) != 2) {
  292. printk(KERN_ERR "fsl-sata: wrong parameter format.\n");
  293. return -EINVAL;
  294. }
  295. fsl_sata_set_irq_coalescing(dev_get_drvdata(dev),
  296. coalescing_count, coalescing_ticks);
  297. return strlen(buf);
  298. }
  299. static ssize_t fsl_sata_rx_watermark_show(struct device *dev,
  300. struct device_attribute *attr, char *buf)
  301. {
  302. unsigned int rx_watermark;
  303. unsigned long flags;
  304. struct ata_host *host = dev_get_drvdata(dev);
  305. struct sata_fsl_host_priv *host_priv = host->private_data;
  306. void __iomem *csr_base = host_priv->csr_base;
  307. spin_lock_irqsave(&host->lock, flags);
  308. rx_watermark = ioread32(csr_base + TRANSCFG);
  309. rx_watermark &= 0x1f;
  310. spin_unlock_irqrestore(&host->lock, flags);
  311. return sprintf(buf, "%d\n", rx_watermark);
  312. }
  313. static ssize_t fsl_sata_rx_watermark_store(struct device *dev,
  314. struct device_attribute *attr,
  315. const char *buf, size_t count)
  316. {
  317. unsigned int rx_watermark;
  318. unsigned long flags;
  319. struct ata_host *host = dev_get_drvdata(dev);
  320. struct sata_fsl_host_priv *host_priv = host->private_data;
  321. void __iomem *csr_base = host_priv->csr_base;
  322. u32 temp;
  323. if (sscanf(buf, "%d", &rx_watermark) != 1) {
  324. printk(KERN_ERR "fsl-sata: wrong parameter format.\n");
  325. return -EINVAL;
  326. }
  327. spin_lock_irqsave(&host->lock, flags);
  328. temp = ioread32(csr_base + TRANSCFG);
  329. temp &= 0xffffffe0;
  330. iowrite32(temp | rx_watermark, csr_base + TRANSCFG);
  331. spin_unlock_irqrestore(&host->lock, flags);
  332. return strlen(buf);
  333. }
  334. static inline unsigned int sata_fsl_tag(unsigned int tag,
  335. void __iomem *hcr_base)
  336. {
  337. /* We let libATA core do actual (queue) tag allocation */
  338. /* all non NCQ/queued commands should have tag#0 */
  339. if (ata_tag_internal(tag)) {
  340. DPRINTK("mapping internal cmds to tag#0\n");
  341. return 0;
  342. }
  343. if (unlikely(tag >= SATA_FSL_QUEUE_DEPTH)) {
  344. DPRINTK("tag %d invalid : out of range\n", tag);
  345. return 0;
  346. }
  347. if (unlikely((ioread32(hcr_base + CQ)) & (1 << tag))) {
  348. DPRINTK("tag %d invalid : in use!!\n", tag);
  349. return 0;
  350. }
  351. return tag;
  352. }
  353. static void sata_fsl_setup_cmd_hdr_entry(struct sata_fsl_port_priv *pp,
  354. unsigned int tag, u32 desc_info,
  355. u32 data_xfer_len, u8 num_prde,
  356. u8 fis_len)
  357. {
  358. dma_addr_t cmd_descriptor_address;
  359. cmd_descriptor_address = pp->cmdentry_paddr +
  360. tag * SATA_FSL_CMD_DESC_SIZE;
  361. /* NOTE: both data_xfer_len & fis_len are Dword counts */
  362. pp->cmdslot[tag].cda = cpu_to_le32(cmd_descriptor_address);
  363. pp->cmdslot[tag].prde_fis_len =
  364. cpu_to_le32((num_prde << 16) | (fis_len << 2));
  365. pp->cmdslot[tag].ttl = cpu_to_le32(data_xfer_len & ~0x03);
  366. pp->cmdslot[tag].desc_info = cpu_to_le32(desc_info | (tag & 0x1F));
  367. VPRINTK("cda=0x%x, prde_fis_len=0x%x, ttl=0x%x, di=0x%x\n",
  368. pp->cmdslot[tag].cda,
  369. pp->cmdslot[tag].prde_fis_len,
  370. pp->cmdslot[tag].ttl, pp->cmdslot[tag].desc_info);
  371. }
  372. static unsigned int sata_fsl_fill_sg(struct ata_queued_cmd *qc, void *cmd_desc,
  373. u32 *ttl, dma_addr_t cmd_desc_paddr,
  374. int data_snoop)
  375. {
  376. struct scatterlist *sg;
  377. unsigned int num_prde = 0;
  378. u32 ttl_dwords = 0;
  379. /*
  380. * NOTE : direct & indirect prdt's are contiguously allocated
  381. */
  382. struct prde *prd = (struct prde *)&((struct command_desc *)
  383. cmd_desc)->prdt;
  384. struct prde *prd_ptr_to_indirect_ext = NULL;
  385. unsigned indirect_ext_segment_sz = 0;
  386. dma_addr_t indirect_ext_segment_paddr;
  387. unsigned int si;
  388. VPRINTK("SATA FSL : cd = 0x%p, prd = 0x%p\n", cmd_desc, prd);
  389. indirect_ext_segment_paddr = cmd_desc_paddr +
  390. SATA_FSL_CMD_DESC_OFFSET_TO_PRDT + SATA_FSL_MAX_PRD_DIRECT * 16;
  391. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  392. dma_addr_t sg_addr = sg_dma_address(sg);
  393. u32 sg_len = sg_dma_len(sg);
  394. VPRINTK("SATA FSL : fill_sg, sg_addr = 0x%llx, sg_len = %d\n",
  395. (unsigned long long)sg_addr, sg_len);
  396. /* warn if each s/g element is not dword aligned */
  397. if (unlikely(sg_addr & 0x03))
  398. ata_port_err(qc->ap, "s/g addr unaligned : 0x%llx\n",
  399. (unsigned long long)sg_addr);
  400. if (unlikely(sg_len & 0x03))
  401. ata_port_err(qc->ap, "s/g len unaligned : 0x%x\n",
  402. sg_len);
  403. if (num_prde == (SATA_FSL_MAX_PRD_DIRECT - 1) &&
  404. sg_next(sg) != NULL) {
  405. VPRINTK("setting indirect prde\n");
  406. prd_ptr_to_indirect_ext = prd;
  407. prd->dba = cpu_to_le32(indirect_ext_segment_paddr);
  408. indirect_ext_segment_sz = 0;
  409. ++prd;
  410. ++num_prde;
  411. }
  412. ttl_dwords += sg_len;
  413. prd->dba = cpu_to_le32(sg_addr);
  414. prd->ddc_and_ext = cpu_to_le32(data_snoop | (sg_len & ~0x03));
  415. VPRINTK("sg_fill, ttl=%d, dba=0x%x, ddc=0x%x\n",
  416. ttl_dwords, prd->dba, prd->ddc_and_ext);
  417. ++num_prde;
  418. ++prd;
  419. if (prd_ptr_to_indirect_ext)
  420. indirect_ext_segment_sz += sg_len;
  421. }
  422. if (prd_ptr_to_indirect_ext) {
  423. /* set indirect extension flag along with indirect ext. size */
  424. prd_ptr_to_indirect_ext->ddc_and_ext =
  425. cpu_to_le32((EXT_INDIRECT_SEG_PRD_FLAG |
  426. data_snoop |
  427. (indirect_ext_segment_sz & ~0x03)));
  428. }
  429. *ttl = ttl_dwords;
  430. return num_prde;
  431. }
  432. static void sata_fsl_qc_prep(struct ata_queued_cmd *qc)
  433. {
  434. struct ata_port *ap = qc->ap;
  435. struct sata_fsl_port_priv *pp = ap->private_data;
  436. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  437. void __iomem *hcr_base = host_priv->hcr_base;
  438. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  439. struct command_desc *cd;
  440. u32 desc_info = CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE;
  441. u32 num_prde = 0;
  442. u32 ttl_dwords = 0;
  443. dma_addr_t cd_paddr;
  444. cd = (struct command_desc *)pp->cmdentry + tag;
  445. cd_paddr = pp->cmdentry_paddr + tag * SATA_FSL_CMD_DESC_SIZE;
  446. ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, (u8 *) &cd->cfis);
  447. VPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x\n",
  448. cd->cfis[0], cd->cfis[1], cd->cfis[2]);
  449. if (qc->tf.protocol == ATA_PROT_NCQ) {
  450. VPRINTK("FPDMA xfer,Sctor cnt[0:7],[8:15] = %d,%d\n",
  451. cd->cfis[3], cd->cfis[11]);
  452. }
  453. /* setup "ACMD - atapi command" in cmd. desc. if this is ATAPI cmd */
  454. if (ata_is_atapi(qc->tf.protocol)) {
  455. desc_info |= ATAPI_CMD;
  456. memset((void *)&cd->acmd, 0, 32);
  457. memcpy((void *)&cd->acmd, qc->cdb, qc->dev->cdb_len);
  458. }
  459. if (qc->flags & ATA_QCFLAG_DMAMAP)
  460. num_prde = sata_fsl_fill_sg(qc, (void *)cd,
  461. &ttl_dwords, cd_paddr,
  462. host_priv->data_snoop);
  463. if (qc->tf.protocol == ATA_PROT_NCQ)
  464. desc_info |= FPDMA_QUEUED_CMD;
  465. sata_fsl_setup_cmd_hdr_entry(pp, tag, desc_info, ttl_dwords,
  466. num_prde, 5);
  467. VPRINTK("SATA FSL : xx_qc_prep, di = 0x%x, ttl = %d, num_prde = %d\n",
  468. desc_info, ttl_dwords, num_prde);
  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->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->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_alloc_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ, &mem_dma,
  609. GFP_KERNEL);
  610. if (!mem) {
  611. kfree(pp);
  612. return -ENOMEM;
  613. }
  614. memset(mem, 0, SATA_FSL_PORT_PRIV_DMA_SZ);
  615. pp->cmdslot = mem;
  616. pp->cmdslot_paddr = mem_dma;
  617. mem += SATA_FSL_CMD_SLOT_SIZE;
  618. mem_dma += SATA_FSL_CMD_SLOT_SIZE;
  619. pp->cmdentry = mem;
  620. pp->cmdentry_paddr = mem_dma;
  621. ap->private_data = pp;
  622. VPRINTK("CHBA = 0x%x, cmdentry_phys = 0x%x\n",
  623. pp->cmdslot_paddr, pp->cmdentry_paddr);
  624. /* Now, update the CHBA register in host controller cmd register set */
  625. iowrite32(pp->cmdslot_paddr & 0xffffffff, hcr_base + CHBA);
  626. /*
  627. * Now, we can bring the controller on-line & also initiate
  628. * the COMINIT sequence, we simply return here and the boot-probing
  629. * & device discovery process is re-initiated by libATA using a
  630. * Softreset EH (dummy) session. Hence, boot probing and device
  631. * discovey will be part of sata_fsl_softreset() callback.
  632. */
  633. temp = ioread32(hcr_base + HCONTROL);
  634. iowrite32((temp | HCONTROL_ONLINE_PHY_RST), hcr_base + HCONTROL);
  635. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  636. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  637. VPRINTK("CHBA = 0x%x\n", ioread32(hcr_base + CHBA));
  638. #ifdef CONFIG_MPC8315_DS
  639. /*
  640. * Workaround for 8315DS board 3gbps link-up issue,
  641. * currently limit SATA port to GEN1 speed
  642. */
  643. sata_fsl_scr_read(&ap->link, SCR_CONTROL, &temp);
  644. temp &= ~(0xF << 4);
  645. temp |= (0x1 << 4);
  646. sata_fsl_scr_write(&ap->link, SCR_CONTROL, temp);
  647. sata_fsl_scr_read(&ap->link, SCR_CONTROL, &temp);
  648. dev_warn(dev, "scr_control, speed limited to %x\n", temp);
  649. #endif
  650. return 0;
  651. }
  652. static void sata_fsl_port_stop(struct ata_port *ap)
  653. {
  654. struct device *dev = ap->host->dev;
  655. struct sata_fsl_port_priv *pp = ap->private_data;
  656. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  657. void __iomem *hcr_base = host_priv->hcr_base;
  658. u32 temp;
  659. /*
  660. * Force host controller to go off-line, aborting current operations
  661. */
  662. temp = ioread32(hcr_base + HCONTROL);
  663. temp &= ~HCONTROL_ONLINE_PHY_RST;
  664. temp |= HCONTROL_FORCE_OFFLINE;
  665. iowrite32(temp, hcr_base + HCONTROL);
  666. /* Poll for controller to go offline - should happen immediately */
  667. ata_wait_register(ap, hcr_base + HSTATUS, ONLINE, ONLINE, 1, 1);
  668. ap->private_data = NULL;
  669. dma_free_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ,
  670. pp->cmdslot, pp->cmdslot_paddr);
  671. kfree(pp);
  672. }
  673. static unsigned int sata_fsl_dev_classify(struct ata_port *ap)
  674. {
  675. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  676. void __iomem *hcr_base = host_priv->hcr_base;
  677. struct ata_taskfile tf;
  678. u32 temp;
  679. temp = ioread32(hcr_base + SIGNATURE);
  680. VPRINTK("raw sig = 0x%x\n", temp);
  681. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  682. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  683. tf.lbah = (temp >> 24) & 0xff;
  684. tf.lbam = (temp >> 16) & 0xff;
  685. tf.lbal = (temp >> 8) & 0xff;
  686. tf.nsect = temp & 0xff;
  687. return ata_dev_classify(&tf);
  688. }
  689. static int sata_fsl_hardreset(struct ata_link *link, unsigned int *class,
  690. unsigned long deadline)
  691. {
  692. struct ata_port *ap = link->ap;
  693. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  694. void __iomem *hcr_base = host_priv->hcr_base;
  695. u32 temp;
  696. int i = 0;
  697. unsigned long start_jiffies;
  698. DPRINTK("in xx_hardreset\n");
  699. try_offline_again:
  700. /*
  701. * Force host controller to go off-line, aborting current operations
  702. */
  703. temp = ioread32(hcr_base + HCONTROL);
  704. temp &= ~HCONTROL_ONLINE_PHY_RST;
  705. iowrite32(temp, hcr_base + HCONTROL);
  706. /* Poll for controller to go offline */
  707. temp = ata_wait_register(ap, hcr_base + HSTATUS, ONLINE, ONLINE,
  708. 1, 500);
  709. if (temp & ONLINE) {
  710. ata_port_err(ap, "Hardreset failed, not off-lined %d\n", i);
  711. /*
  712. * Try to offline controller atleast twice
  713. */
  714. i++;
  715. if (i == 2)
  716. goto err;
  717. else
  718. goto try_offline_again;
  719. }
  720. DPRINTK("hardreset, controller off-lined\n");
  721. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  722. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  723. /*
  724. * PHY reset should remain asserted for atleast 1ms
  725. */
  726. ata_msleep(ap, 1);
  727. /*
  728. * Now, bring the host controller online again, this can take time
  729. * as PHY reset and communication establishment, 1st D2H FIS and
  730. * device signature update is done, on safe side assume 500ms
  731. * NOTE : Host online status may be indicated immediately!!
  732. */
  733. temp = ioread32(hcr_base + HCONTROL);
  734. temp |= (HCONTROL_ONLINE_PHY_RST | HCONTROL_SNOOP_ENABLE);
  735. temp |= HCONTROL_PMP_ATTACHED;
  736. iowrite32(temp, hcr_base + HCONTROL);
  737. temp = ata_wait_register(ap, hcr_base + HSTATUS, ONLINE, 0, 1, 500);
  738. if (!(temp & ONLINE)) {
  739. ata_port_err(ap, "Hardreset failed, not on-lined\n");
  740. goto err;
  741. }
  742. DPRINTK("hardreset, controller off-lined & on-lined\n");
  743. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  744. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  745. /*
  746. * First, wait for the PHYRDY change to occur before waiting for
  747. * the signature, and also verify if SStatus indicates device
  748. * presence
  749. */
  750. temp = ata_wait_register(ap, hcr_base + HSTATUS, 0xFF, 0, 1, 500);
  751. if ((!(temp & 0x10)) || ata_link_offline(link)) {
  752. ata_port_warn(ap, "No Device OR PHYRDY change,Hstatus = 0x%x\n",
  753. ioread32(hcr_base + HSTATUS));
  754. *class = ATA_DEV_NONE;
  755. return 0;
  756. }
  757. /*
  758. * Wait for the first D2H from device,i.e,signature update notification
  759. */
  760. start_jiffies = jiffies;
  761. temp = ata_wait_register(ap, hcr_base + HSTATUS, 0xFF, 0x10,
  762. 500, jiffies_to_msecs(deadline - start_jiffies));
  763. if ((temp & 0xFF) != 0x18) {
  764. ata_port_warn(ap, "No Signature Update\n");
  765. *class = ATA_DEV_NONE;
  766. goto do_followup_srst;
  767. } else {
  768. ata_port_info(ap, "Signature Update detected @ %d msecs\n",
  769. jiffies_to_msecs(jiffies - start_jiffies));
  770. *class = sata_fsl_dev_classify(ap);
  771. return 0;
  772. }
  773. do_followup_srst:
  774. /*
  775. * request libATA to perform follow-up softreset
  776. */
  777. return -EAGAIN;
  778. err:
  779. return -EIO;
  780. }
  781. static int sata_fsl_softreset(struct ata_link *link, unsigned int *class,
  782. unsigned long deadline)
  783. {
  784. struct ata_port *ap = link->ap;
  785. struct sata_fsl_port_priv *pp = ap->private_data;
  786. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  787. void __iomem *hcr_base = host_priv->hcr_base;
  788. int pmp = sata_srst_pmp(link);
  789. u32 temp;
  790. struct ata_taskfile tf;
  791. u8 *cfis;
  792. u32 Serror;
  793. DPRINTK("in xx_softreset\n");
  794. if (ata_link_offline(link)) {
  795. DPRINTK("PHY reports no device\n");
  796. *class = ATA_DEV_NONE;
  797. return 0;
  798. }
  799. /*
  800. * Send a device reset (SRST) explicitly on command slot #0
  801. * Check : will the command queue (reg) be cleared during offlining ??
  802. * Also we will be online only if Phy commn. has been established
  803. * and device presence has been detected, therefore if we have
  804. * reached here, we can send a command to the target device
  805. */
  806. DPRINTK("Sending SRST/device reset\n");
  807. ata_tf_init(link->device, &tf);
  808. cfis = (u8 *) &pp->cmdentry->cfis;
  809. /* device reset/SRST is a control register update FIS, uses tag0 */
  810. sata_fsl_setup_cmd_hdr_entry(pp, 0,
  811. SRST_CMD | CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
  812. tf.ctl |= ATA_SRST; /* setup SRST bit in taskfile control reg */
  813. ata_tf_to_fis(&tf, pmp, 0, cfis);
  814. DPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x, 0x%x\n",
  815. cfis[0], cfis[1], cfis[2], cfis[3]);
  816. /*
  817. * Queue SRST command to the controller/device, ensure that no
  818. * other commands are active on the controller/device
  819. */
  820. DPRINTK("@Softreset, CQ = 0x%x, CA = 0x%x, CC = 0x%x\n",
  821. ioread32(CQ + hcr_base),
  822. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  823. iowrite32(0xFFFF, CC + hcr_base);
  824. if (pmp != SATA_PMP_CTRL_PORT)
  825. iowrite32(pmp, CQPMP + hcr_base);
  826. iowrite32(1, CQ + hcr_base);
  827. temp = ata_wait_register(ap, CQ + hcr_base, 0x1, 0x1, 1, 5000);
  828. if (temp & 0x1) {
  829. ata_port_warn(ap, "ATA_SRST issue failed\n");
  830. DPRINTK("Softreset@5000,CQ=0x%x,CA=0x%x,CC=0x%x\n",
  831. ioread32(CQ + hcr_base),
  832. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  833. sata_fsl_scr_read(&ap->link, SCR_ERROR, &Serror);
  834. DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  835. DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  836. DPRINTK("Serror = 0x%x\n", Serror);
  837. goto err;
  838. }
  839. ata_msleep(ap, 1);
  840. /*
  841. * SATA device enters reset state after receiving a Control register
  842. * FIS with SRST bit asserted and it awaits another H2D Control reg.
  843. * FIS with SRST bit cleared, then the device does internal diags &
  844. * initialization, followed by indicating it's initialization status
  845. * using ATA signature D2H register FIS to the host controller.
  846. */
  847. sata_fsl_setup_cmd_hdr_entry(pp, 0, CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE,
  848. 0, 0, 5);
  849. tf.ctl &= ~ATA_SRST; /* 2nd H2D Ctl. register FIS */
  850. ata_tf_to_fis(&tf, pmp, 0, cfis);
  851. if (pmp != SATA_PMP_CTRL_PORT)
  852. iowrite32(pmp, CQPMP + hcr_base);
  853. iowrite32(1, CQ + hcr_base);
  854. ata_msleep(ap, 150); /* ?? */
  855. /*
  856. * The above command would have signalled an interrupt on command
  857. * complete, which needs special handling, by clearing the Nth
  858. * command bit of the CCreg
  859. */
  860. iowrite32(0x01, CC + hcr_base); /* We know it will be cmd#0 always */
  861. DPRINTK("SATA FSL : Now checking device signature\n");
  862. *class = ATA_DEV_NONE;
  863. /* Verify if SStatus indicates device presence */
  864. if (ata_link_online(link)) {
  865. /*
  866. * if we are here, device presence has been detected,
  867. * 1st D2H FIS would have been received, but sfis in
  868. * command desc. is not updated, but signature register
  869. * would have been updated
  870. */
  871. *class = sata_fsl_dev_classify(ap);
  872. DPRINTK("class = %d\n", *class);
  873. VPRINTK("ccreg = 0x%x\n", ioread32(hcr_base + CC));
  874. VPRINTK("cereg = 0x%x\n", ioread32(hcr_base + CE));
  875. }
  876. return 0;
  877. err:
  878. return -EIO;
  879. }
  880. static void sata_fsl_error_handler(struct ata_port *ap)
  881. {
  882. DPRINTK("in xx_error_handler\n");
  883. sata_pmp_error_handler(ap);
  884. }
  885. static void sata_fsl_post_internal_cmd(struct ata_queued_cmd *qc)
  886. {
  887. if (qc->flags & ATA_QCFLAG_FAILED)
  888. qc->err_mask |= AC_ERR_OTHER;
  889. if (qc->err_mask) {
  890. /* make DMA engine forget about the failed command */
  891. }
  892. }
  893. static void sata_fsl_error_intr(struct ata_port *ap)
  894. {
  895. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  896. void __iomem *hcr_base = host_priv->hcr_base;
  897. u32 hstatus, dereg=0, cereg = 0, SError = 0;
  898. unsigned int err_mask = 0, action = 0;
  899. int freeze = 0, abort=0;
  900. struct ata_link *link = NULL;
  901. struct ata_queued_cmd *qc = NULL;
  902. struct ata_eh_info *ehi;
  903. hstatus = ioread32(hcr_base + HSTATUS);
  904. cereg = ioread32(hcr_base + CE);
  905. /* first, analyze and record host port events */
  906. link = &ap->link;
  907. ehi = &link->eh_info;
  908. ata_ehi_clear_desc(ehi);
  909. /*
  910. * Handle & Clear SError
  911. */
  912. sata_fsl_scr_read(&ap->link, SCR_ERROR, &SError);
  913. if (unlikely(SError & 0xFFFF0000))
  914. sata_fsl_scr_write(&ap->link, SCR_ERROR, SError);
  915. DPRINTK("error_intr,hStat=0x%x,CE=0x%x,DE =0x%x,SErr=0x%x\n",
  916. hstatus, cereg, ioread32(hcr_base + DE), SError);
  917. /* handle fatal errors */
  918. if (hstatus & FATAL_ERROR_DECODE) {
  919. ehi->err_mask |= AC_ERR_ATA_BUS;
  920. ehi->action |= ATA_EH_SOFTRESET;
  921. freeze = 1;
  922. }
  923. /* Handle SDB FIS receive & notify update */
  924. if (hstatus & INT_ON_SNOTIFY_UPDATE)
  925. sata_async_notification(ap);
  926. /* Handle PHYRDY change notification */
  927. if (hstatus & INT_ON_PHYRDY_CHG) {
  928. DPRINTK("SATA FSL: PHYRDY change indication\n");
  929. /* Setup a soft-reset EH action */
  930. ata_ehi_hotplugged(ehi);
  931. ata_ehi_push_desc(ehi, "%s", "PHY RDY changed");
  932. freeze = 1;
  933. }
  934. /* handle single device errors */
  935. if (cereg) {
  936. /*
  937. * clear the command error, also clears queue to the device
  938. * in error, and we can (re)issue commands to this device.
  939. * When a device is in error all commands queued into the
  940. * host controller and at the device are considered aborted
  941. * and the queue for that device is stopped. Now, after
  942. * clearing the device error, we can issue commands to the
  943. * device to interrogate it to find the source of the error.
  944. */
  945. abort = 1;
  946. DPRINTK("single device error, CE=0x%x, DE=0x%x\n",
  947. ioread32(hcr_base + CE), ioread32(hcr_base + DE));
  948. /* find out the offending link and qc */
  949. if (ap->nr_pmp_links) {
  950. unsigned int dev_num;
  951. dereg = ioread32(hcr_base + DE);
  952. iowrite32(dereg, hcr_base + DE);
  953. iowrite32(cereg, hcr_base + CE);
  954. dev_num = ffs(dereg) - 1;
  955. if (dev_num < ap->nr_pmp_links && dereg != 0) {
  956. link = &ap->pmp_link[dev_num];
  957. ehi = &link->eh_info;
  958. qc = ata_qc_from_tag(ap, link->active_tag);
  959. /*
  960. * We should consider this as non fatal error,
  961. * and TF must be updated as done below.
  962. */
  963. err_mask |= AC_ERR_DEV;
  964. } else {
  965. err_mask |= AC_ERR_HSM;
  966. action |= ATA_EH_HARDRESET;
  967. freeze = 1;
  968. }
  969. } else {
  970. dereg = ioread32(hcr_base + DE);
  971. iowrite32(dereg, hcr_base + DE);
  972. iowrite32(cereg, hcr_base + CE);
  973. qc = ata_qc_from_tag(ap, link->active_tag);
  974. /*
  975. * We should consider this as non fatal error,
  976. * and TF must be updated as done below.
  977. */
  978. err_mask |= AC_ERR_DEV;
  979. }
  980. }
  981. /* record error info */
  982. if (qc)
  983. qc->err_mask |= err_mask;
  984. else
  985. ehi->err_mask |= err_mask;
  986. ehi->action |= action;
  987. /* freeze or abort */
  988. if (freeze)
  989. ata_port_freeze(ap);
  990. else if (abort) {
  991. if (qc)
  992. ata_link_abort(qc->dev->link);
  993. else
  994. ata_port_abort(ap);
  995. }
  996. }
  997. static void sata_fsl_host_intr(struct ata_port *ap)
  998. {
  999. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  1000. void __iomem *hcr_base = host_priv->hcr_base;
  1001. u32 hstatus, done_mask = 0;
  1002. struct ata_queued_cmd *qc;
  1003. u32 SError;
  1004. u32 tag;
  1005. u32 status_mask = INT_ON_ERROR;
  1006. hstatus = ioread32(hcr_base + HSTATUS);
  1007. sata_fsl_scr_read(&ap->link, SCR_ERROR, &SError);
  1008. /* Read command completed register */
  1009. done_mask = ioread32(hcr_base + CC);
  1010. /* Workaround for data length mismatch errata */
  1011. if (unlikely(hstatus & INT_ON_DATA_LENGTH_MISMATCH)) {
  1012. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1013. qc = ata_qc_from_tag(ap, tag);
  1014. if (qc && ata_is_atapi(qc->tf.protocol)) {
  1015. u32 hcontrol;
  1016. /* Set HControl[27] to clear error registers */
  1017. hcontrol = ioread32(hcr_base + HCONTROL);
  1018. iowrite32(hcontrol | CLEAR_ERROR,
  1019. hcr_base + HCONTROL);
  1020. /* Clear HControl[27] */
  1021. iowrite32(hcontrol & ~CLEAR_ERROR,
  1022. hcr_base + HCONTROL);
  1023. /* Clear SError[E] bit */
  1024. sata_fsl_scr_write(&ap->link, SCR_ERROR,
  1025. SError);
  1026. /* Ignore fatal error and device error */
  1027. status_mask &= ~(INT_ON_SINGL_DEVICE_ERR
  1028. | INT_ON_FATAL_ERR);
  1029. break;
  1030. }
  1031. }
  1032. }
  1033. if (unlikely(SError & 0xFFFF0000)) {
  1034. DPRINTK("serror @host_intr : 0x%x\n", SError);
  1035. sata_fsl_error_intr(ap);
  1036. }
  1037. if (unlikely(hstatus & status_mask)) {
  1038. DPRINTK("error interrupt!!\n");
  1039. sata_fsl_error_intr(ap);
  1040. return;
  1041. }
  1042. VPRINTK("Status of all queues :\n");
  1043. VPRINTK("done_mask/CC = 0x%x, CA = 0x%x, CE=0x%x,CQ=0x%x,apqa=0x%x\n",
  1044. done_mask,
  1045. ioread32(hcr_base + CA),
  1046. ioread32(hcr_base + CE),
  1047. ioread32(hcr_base + CQ),
  1048. ap->qc_active);
  1049. if (done_mask & ap->qc_active) {
  1050. int i;
  1051. /* clear CC bit, this will also complete the interrupt */
  1052. iowrite32(done_mask, hcr_base + CC);
  1053. DPRINTK("Status of all queues :\n");
  1054. DPRINTK("done_mask/CC = 0x%x, CA = 0x%x, CE=0x%x\n",
  1055. done_mask, ioread32(hcr_base + CA),
  1056. ioread32(hcr_base + CE));
  1057. for (i = 0; i < SATA_FSL_QUEUE_DEPTH; i++) {
  1058. if (done_mask & (1 << i))
  1059. DPRINTK
  1060. ("completing ncq cmd,tag=%d,CC=0x%x,CA=0x%x\n",
  1061. i, ioread32(hcr_base + CC),
  1062. ioread32(hcr_base + CA));
  1063. }
  1064. ata_qc_complete_multiple(ap, ap->qc_active ^ done_mask);
  1065. return;
  1066. } else if ((ap->qc_active & (1 << ATA_TAG_INTERNAL))) {
  1067. iowrite32(1, hcr_base + CC);
  1068. qc = ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
  1069. DPRINTK("completing non-ncq cmd, CC=0x%x\n",
  1070. ioread32(hcr_base + CC));
  1071. if (qc) {
  1072. ata_qc_complete(qc);
  1073. }
  1074. } else {
  1075. /* Spurious Interrupt!! */
  1076. DPRINTK("spurious interrupt!!, CC = 0x%x\n",
  1077. ioread32(hcr_base + CC));
  1078. iowrite32(done_mask, hcr_base + CC);
  1079. return;
  1080. }
  1081. }
  1082. static irqreturn_t sata_fsl_interrupt(int irq, void *dev_instance)
  1083. {
  1084. struct ata_host *host = dev_instance;
  1085. struct sata_fsl_host_priv *host_priv = host->private_data;
  1086. void __iomem *hcr_base = host_priv->hcr_base;
  1087. u32 interrupt_enables;
  1088. unsigned handled = 0;
  1089. struct ata_port *ap;
  1090. /* ack. any pending IRQs for this controller/port */
  1091. interrupt_enables = ioread32(hcr_base + HSTATUS);
  1092. interrupt_enables &= 0x3F;
  1093. DPRINTK("interrupt status 0x%x\n", interrupt_enables);
  1094. if (!interrupt_enables)
  1095. return IRQ_NONE;
  1096. spin_lock(&host->lock);
  1097. /* Assuming one port per host controller */
  1098. ap = host->ports[0];
  1099. if (ap) {
  1100. sata_fsl_host_intr(ap);
  1101. } else {
  1102. dev_warn(host->dev, "interrupt on disabled port 0\n");
  1103. }
  1104. iowrite32(interrupt_enables, hcr_base + HSTATUS);
  1105. handled = 1;
  1106. spin_unlock(&host->lock);
  1107. return IRQ_RETVAL(handled);
  1108. }
  1109. /*
  1110. * Multiple ports are represented by multiple SATA controllers with
  1111. * one port per controller
  1112. */
  1113. static int sata_fsl_init_controller(struct ata_host *host)
  1114. {
  1115. struct sata_fsl_host_priv *host_priv = host->private_data;
  1116. void __iomem *hcr_base = host_priv->hcr_base;
  1117. u32 temp;
  1118. /*
  1119. * NOTE : We cannot bring the controller online before setting
  1120. * the CHBA, hence main controller initialization is done as
  1121. * part of the port_start() callback
  1122. */
  1123. /* sata controller to operate in enterprise mode */
  1124. temp = ioread32(hcr_base + HCONTROL);
  1125. iowrite32(temp & ~HCONTROL_LEGACY, hcr_base + HCONTROL);
  1126. /* ack. any pending IRQs for this controller/port */
  1127. temp = ioread32(hcr_base + HSTATUS);
  1128. if (temp & 0x3F)
  1129. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  1130. /* Keep interrupts disabled on the controller */
  1131. temp = ioread32(hcr_base + HCONTROL);
  1132. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  1133. /* Disable interrupt coalescing control(icc), for the moment */
  1134. DPRINTK("icc = 0x%x\n", ioread32(hcr_base + ICC));
  1135. iowrite32(0x01000000, hcr_base + ICC);
  1136. /* clear error registers, SError is cleared by libATA */
  1137. iowrite32(0x00000FFFF, hcr_base + CE);
  1138. iowrite32(0x00000FFFF, hcr_base + DE);
  1139. /*
  1140. * reset the number of command complete bits which will cause the
  1141. * interrupt to be signaled
  1142. */
  1143. fsl_sata_set_irq_coalescing(host, intr_coalescing_count,
  1144. intr_coalescing_ticks);
  1145. /*
  1146. * host controller will be brought on-line, during xx_port_start()
  1147. * callback, that should also initiate the OOB, COMINIT sequence
  1148. */
  1149. DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  1150. DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  1151. return 0;
  1152. }
  1153. /*
  1154. * scsi mid-layer and libata interface structures
  1155. */
  1156. static struct scsi_host_template sata_fsl_sht = {
  1157. ATA_NCQ_SHT("sata_fsl"),
  1158. .can_queue = SATA_FSL_QUEUE_DEPTH,
  1159. .sg_tablesize = SATA_FSL_MAX_PRD_USABLE,
  1160. .dma_boundary = ATA_DMA_BOUNDARY,
  1161. };
  1162. static struct ata_port_operations sata_fsl_ops = {
  1163. .inherits = &sata_pmp_port_ops,
  1164. .qc_defer = ata_std_qc_defer,
  1165. .qc_prep = sata_fsl_qc_prep,
  1166. .qc_issue = sata_fsl_qc_issue,
  1167. .qc_fill_rtf = sata_fsl_qc_fill_rtf,
  1168. .scr_read = sata_fsl_scr_read,
  1169. .scr_write = sata_fsl_scr_write,
  1170. .freeze = sata_fsl_freeze,
  1171. .thaw = sata_fsl_thaw,
  1172. .softreset = sata_fsl_softreset,
  1173. .hardreset = sata_fsl_hardreset,
  1174. .pmp_softreset = sata_fsl_softreset,
  1175. .error_handler = sata_fsl_error_handler,
  1176. .post_internal_cmd = sata_fsl_post_internal_cmd,
  1177. .port_start = sata_fsl_port_start,
  1178. .port_stop = sata_fsl_port_stop,
  1179. .pmp_attach = sata_fsl_pmp_attach,
  1180. .pmp_detach = sata_fsl_pmp_detach,
  1181. };
  1182. static const struct ata_port_info sata_fsl_port_info[] = {
  1183. {
  1184. .flags = SATA_FSL_HOST_FLAGS,
  1185. .pio_mask = ATA_PIO4,
  1186. .udma_mask = ATA_UDMA6,
  1187. .port_ops = &sata_fsl_ops,
  1188. },
  1189. };
  1190. static int sata_fsl_probe(struct platform_device *ofdev)
  1191. {
  1192. int retval = -ENXIO;
  1193. void __iomem *hcr_base = NULL;
  1194. void __iomem *ssr_base = NULL;
  1195. void __iomem *csr_base = NULL;
  1196. struct sata_fsl_host_priv *host_priv = NULL;
  1197. int irq;
  1198. struct ata_host *host = NULL;
  1199. u32 temp;
  1200. struct ata_port_info pi = sata_fsl_port_info[0];
  1201. const struct ata_port_info *ppi[] = { &pi, NULL };
  1202. dev_info(&ofdev->dev, "Sata FSL Platform/CSB Driver init\n");
  1203. hcr_base = of_iomap(ofdev->dev.of_node, 0);
  1204. if (!hcr_base)
  1205. goto error_exit_with_cleanup;
  1206. ssr_base = hcr_base + 0x100;
  1207. csr_base = hcr_base + 0x140;
  1208. if (!of_device_is_compatible(ofdev->dev.of_node, "fsl,mpc8315-sata")) {
  1209. temp = ioread32(csr_base + TRANSCFG);
  1210. temp = temp & 0xffffffe0;
  1211. iowrite32(temp | TRANSCFG_RX_WATER_MARK, csr_base + TRANSCFG);
  1212. }
  1213. DPRINTK("@reset i/o = 0x%x\n", ioread32(csr_base + TRANSCFG));
  1214. DPRINTK("sizeof(cmd_desc) = %d\n", sizeof(struct command_desc));
  1215. DPRINTK("sizeof(#define cmd_desc) = %d\n", SATA_FSL_CMD_DESC_SIZE);
  1216. host_priv = kzalloc(sizeof(struct sata_fsl_host_priv), GFP_KERNEL);
  1217. if (!host_priv)
  1218. goto error_exit_with_cleanup;
  1219. host_priv->hcr_base = hcr_base;
  1220. host_priv->ssr_base = ssr_base;
  1221. host_priv->csr_base = csr_base;
  1222. irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
  1223. if (irq < 0) {
  1224. dev_err(&ofdev->dev, "invalid irq from platform\n");
  1225. goto error_exit_with_cleanup;
  1226. }
  1227. host_priv->irq = irq;
  1228. if (of_device_is_compatible(ofdev->dev.of_node, "fsl,pq-sata-v2"))
  1229. host_priv->data_snoop = DATA_SNOOP_ENABLE_V2;
  1230. else
  1231. host_priv->data_snoop = DATA_SNOOP_ENABLE_V1;
  1232. /* allocate host structure */
  1233. host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_FSL_MAX_PORTS);
  1234. if (!host) {
  1235. retval = -ENOMEM;
  1236. goto error_exit_with_cleanup;
  1237. }
  1238. /* host->iomap is not used currently */
  1239. host->private_data = host_priv;
  1240. /* initialize host controller */
  1241. sata_fsl_init_controller(host);
  1242. /*
  1243. * Now, register with libATA core, this will also initiate the
  1244. * device discovery process, invoking our port_start() handler &
  1245. * error_handler() to execute a dummy Softreset EH session
  1246. */
  1247. ata_host_activate(host, irq, sata_fsl_interrupt, SATA_FSL_IRQ_FLAG,
  1248. &sata_fsl_sht);
  1249. dev_set_drvdata(&ofdev->dev, host);
  1250. host_priv->intr_coalescing.show = fsl_sata_intr_coalescing_show;
  1251. host_priv->intr_coalescing.store = fsl_sata_intr_coalescing_store;
  1252. sysfs_attr_init(&host_priv->intr_coalescing.attr);
  1253. host_priv->intr_coalescing.attr.name = "intr_coalescing";
  1254. host_priv->intr_coalescing.attr.mode = S_IRUGO | S_IWUSR;
  1255. retval = device_create_file(host->dev, &host_priv->intr_coalescing);
  1256. if (retval)
  1257. goto error_exit_with_cleanup;
  1258. host_priv->rx_watermark.show = fsl_sata_rx_watermark_show;
  1259. host_priv->rx_watermark.store = fsl_sata_rx_watermark_store;
  1260. sysfs_attr_init(&host_priv->rx_watermark.attr);
  1261. host_priv->rx_watermark.attr.name = "rx_watermark";
  1262. host_priv->rx_watermark.attr.mode = S_IRUGO | S_IWUSR;
  1263. retval = device_create_file(host->dev, &host_priv->rx_watermark);
  1264. if (retval) {
  1265. device_remove_file(&ofdev->dev, &host_priv->intr_coalescing);
  1266. goto error_exit_with_cleanup;
  1267. }
  1268. return 0;
  1269. error_exit_with_cleanup:
  1270. if (host) {
  1271. dev_set_drvdata(&ofdev->dev, NULL);
  1272. ata_host_detach(host);
  1273. }
  1274. if (hcr_base)
  1275. iounmap(hcr_base);
  1276. kfree(host_priv);
  1277. return retval;
  1278. }
  1279. static int sata_fsl_remove(struct platform_device *ofdev)
  1280. {
  1281. struct ata_host *host = dev_get_drvdata(&ofdev->dev);
  1282. struct sata_fsl_host_priv *host_priv = host->private_data;
  1283. device_remove_file(&ofdev->dev, &host_priv->intr_coalescing);
  1284. device_remove_file(&ofdev->dev, &host_priv->rx_watermark);
  1285. ata_host_detach(host);
  1286. dev_set_drvdata(&ofdev->dev, NULL);
  1287. irq_dispose_mapping(host_priv->irq);
  1288. iounmap(host_priv->hcr_base);
  1289. kfree(host_priv);
  1290. return 0;
  1291. }
  1292. #ifdef CONFIG_PM
  1293. static int sata_fsl_suspend(struct platform_device *op, pm_message_t state)
  1294. {
  1295. struct ata_host *host = dev_get_drvdata(&op->dev);
  1296. return ata_host_suspend(host, state);
  1297. }
  1298. static int sata_fsl_resume(struct platform_device *op)
  1299. {
  1300. struct ata_host *host = dev_get_drvdata(&op->dev);
  1301. struct sata_fsl_host_priv *host_priv = host->private_data;
  1302. int ret;
  1303. void __iomem *hcr_base = host_priv->hcr_base;
  1304. struct ata_port *ap = host->ports[0];
  1305. struct sata_fsl_port_priv *pp = ap->private_data;
  1306. ret = sata_fsl_init_controller(host);
  1307. if (ret) {
  1308. dev_err(&op->dev, "Error initializing hardware\n");
  1309. return ret;
  1310. }
  1311. /* Recovery the CHBA register in host controller cmd register set */
  1312. iowrite32(pp->cmdslot_paddr & 0xffffffff, hcr_base + CHBA);
  1313. iowrite32((ioread32(hcr_base + HCONTROL)
  1314. | HCONTROL_ONLINE_PHY_RST
  1315. | HCONTROL_SNOOP_ENABLE
  1316. | HCONTROL_PMP_ATTACHED),
  1317. hcr_base + HCONTROL);
  1318. ata_host_resume(host);
  1319. return 0;
  1320. }
  1321. #endif
  1322. static struct of_device_id fsl_sata_match[] = {
  1323. {
  1324. .compatible = "fsl,pq-sata",
  1325. },
  1326. {
  1327. .compatible = "fsl,pq-sata-v2",
  1328. },
  1329. {},
  1330. };
  1331. MODULE_DEVICE_TABLE(of, fsl_sata_match);
  1332. static struct platform_driver fsl_sata_driver = {
  1333. .driver = {
  1334. .name = "fsl-sata",
  1335. .owner = THIS_MODULE,
  1336. .of_match_table = fsl_sata_match,
  1337. },
  1338. .probe = sata_fsl_probe,
  1339. .remove = sata_fsl_remove,
  1340. #ifdef CONFIG_PM
  1341. .suspend = sata_fsl_suspend,
  1342. .resume = sata_fsl_resume,
  1343. #endif
  1344. };
  1345. module_platform_driver(fsl_sata_driver);
  1346. MODULE_LICENSE("GPL");
  1347. MODULE_AUTHOR("Ashish Kalra, Freescale Semiconductor");
  1348. MODULE_DESCRIPTION("Freescale 3.0Gbps SATA controller low level driver");
  1349. MODULE_VERSION("1.10");