sata_fsl.c 44 KB

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