sata_fsl.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. /*
  2. * drivers/ata/sata_fsl.c
  3. *
  4. * Freescale 3.0Gbps SATA device driver
  5. *
  6. * Author: Ashish Kalra <ashish.kalra@freescale.com>
  7. * Li Yang <leoli@freescale.com>
  8. *
  9. * Copyright (c) 2006-2007 Freescale Semiconductor, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <scsi/scsi_host.h>
  21. #include <scsi/scsi_cmnd.h>
  22. #include <linux/libata.h>
  23. #include <asm/io.h>
  24. #include <linux/of_platform.h>
  25. /* Controller information */
  26. enum {
  27. SATA_FSL_QUEUE_DEPTH = 16,
  28. SATA_FSL_MAX_PRD = 63,
  29. SATA_FSL_MAX_PRD_USABLE = SATA_FSL_MAX_PRD - 1,
  30. SATA_FSL_MAX_PRD_DIRECT = 16, /* Direct PRDT entries */
  31. SATA_FSL_HOST_FLAGS = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  32. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  33. ATA_FLAG_NCQ),
  34. SATA_FSL_HOST_LFLAGS = ATA_LFLAG_SKIP_D2H_BSY,
  35. SATA_FSL_MAX_CMDS = SATA_FSL_QUEUE_DEPTH,
  36. SATA_FSL_CMD_HDR_SIZE = 16, /* 4 DWORDS */
  37. SATA_FSL_CMD_SLOT_SIZE = (SATA_FSL_MAX_CMDS * SATA_FSL_CMD_HDR_SIZE),
  38. /*
  39. * SATA-FSL host controller supports a max. of (15+1) direct PRDEs, and
  40. * chained indirect PRDEs upto a max count of 63.
  41. * We are allocating an array of 63 PRDEs contigiously, but PRDE#15 will
  42. * be setup as an indirect descriptor, pointing to it's next
  43. * (contigious) PRDE. Though chained indirect PRDE arrays are
  44. * supported,it will be more efficient to use a direct PRDT and
  45. * a single chain/link to indirect PRDE array/PRDT.
  46. */
  47. SATA_FSL_CMD_DESC_CFIS_SZ = 32,
  48. SATA_FSL_CMD_DESC_SFIS_SZ = 32,
  49. SATA_FSL_CMD_DESC_ACMD_SZ = 16,
  50. SATA_FSL_CMD_DESC_RSRVD = 16,
  51. SATA_FSL_CMD_DESC_SIZE = (SATA_FSL_CMD_DESC_CFIS_SZ +
  52. SATA_FSL_CMD_DESC_SFIS_SZ +
  53. SATA_FSL_CMD_DESC_ACMD_SZ +
  54. SATA_FSL_CMD_DESC_RSRVD +
  55. SATA_FSL_MAX_PRD * 16),
  56. SATA_FSL_CMD_DESC_OFFSET_TO_PRDT =
  57. (SATA_FSL_CMD_DESC_CFIS_SZ +
  58. SATA_FSL_CMD_DESC_SFIS_SZ +
  59. SATA_FSL_CMD_DESC_ACMD_SZ +
  60. SATA_FSL_CMD_DESC_RSRVD),
  61. SATA_FSL_CMD_DESC_AR_SZ = (SATA_FSL_CMD_DESC_SIZE * SATA_FSL_MAX_CMDS),
  62. SATA_FSL_PORT_PRIV_DMA_SZ = (SATA_FSL_CMD_SLOT_SIZE +
  63. SATA_FSL_CMD_DESC_AR_SZ),
  64. /*
  65. * MPC8315 has two SATA controllers, SATA1 & SATA2
  66. * (one port per controller)
  67. * MPC837x has 2/4 controllers, one port per controller
  68. */
  69. SATA_FSL_MAX_PORTS = 1,
  70. SATA_FSL_IRQ_FLAG = IRQF_SHARED,
  71. };
  72. /*
  73. * Host Controller command register set - per port
  74. */
  75. enum {
  76. CQ = 0,
  77. CA = 8,
  78. CC = 0x10,
  79. CE = 0x18,
  80. DE = 0x20,
  81. CHBA = 0x24,
  82. HSTATUS = 0x28,
  83. HCONTROL = 0x2C,
  84. CQPMP = 0x30,
  85. SIGNATURE = 0x34,
  86. ICC = 0x38,
  87. /*
  88. * Host Status Register (HStatus) bitdefs
  89. */
  90. ONLINE = (1 << 31),
  91. GOING_OFFLINE = (1 << 30),
  92. BIST_ERR = (1 << 29),
  93. FATAL_ERR_HC_MASTER_ERR = (1 << 18),
  94. FATAL_ERR_PARITY_ERR_TX = (1 << 17),
  95. FATAL_ERR_PARITY_ERR_RX = (1 << 16),
  96. FATAL_ERR_DATA_UNDERRUN = (1 << 13),
  97. FATAL_ERR_DATA_OVERRUN = (1 << 12),
  98. FATAL_ERR_CRC_ERR_TX = (1 << 11),
  99. FATAL_ERR_CRC_ERR_RX = (1 << 10),
  100. FATAL_ERR_FIFO_OVRFL_TX = (1 << 9),
  101. FATAL_ERR_FIFO_OVRFL_RX = (1 << 8),
  102. FATAL_ERROR_DECODE = FATAL_ERR_HC_MASTER_ERR |
  103. FATAL_ERR_PARITY_ERR_TX |
  104. FATAL_ERR_PARITY_ERR_RX |
  105. FATAL_ERR_DATA_UNDERRUN |
  106. FATAL_ERR_DATA_OVERRUN |
  107. FATAL_ERR_CRC_ERR_TX |
  108. FATAL_ERR_CRC_ERR_RX |
  109. FATAL_ERR_FIFO_OVRFL_TX | FATAL_ERR_FIFO_OVRFL_RX,
  110. INT_ON_FATAL_ERR = (1 << 5),
  111. INT_ON_PHYRDY_CHG = (1 << 4),
  112. INT_ON_SIGNATURE_UPDATE = (1 << 3),
  113. INT_ON_SNOTIFY_UPDATE = (1 << 2),
  114. INT_ON_SINGL_DEVICE_ERR = (1 << 1),
  115. INT_ON_CMD_COMPLETE = 1,
  116. INT_ON_ERROR = INT_ON_FATAL_ERR |
  117. INT_ON_PHYRDY_CHG | INT_ON_SINGL_DEVICE_ERR,
  118. /*
  119. * Host Control Register (HControl) bitdefs
  120. */
  121. HCONTROL_ONLINE_PHY_RST = (1 << 31),
  122. HCONTROL_FORCE_OFFLINE = (1 << 30),
  123. HCONTROL_PARITY_PROT_MOD = (1 << 14),
  124. HCONTROL_DPATH_PARITY = (1 << 12),
  125. HCONTROL_SNOOP_ENABLE = (1 << 10),
  126. HCONTROL_PMP_ATTACHED = (1 << 9),
  127. HCONTROL_COPYOUT_STATFIS = (1 << 8),
  128. IE_ON_FATAL_ERR = (1 << 5),
  129. IE_ON_PHYRDY_CHG = (1 << 4),
  130. IE_ON_SIGNATURE_UPDATE = (1 << 3),
  131. IE_ON_SNOTIFY_UPDATE = (1 << 2),
  132. IE_ON_SINGL_DEVICE_ERR = (1 << 1),
  133. IE_ON_CMD_COMPLETE = 1,
  134. DEFAULT_PORT_IRQ_ENABLE_MASK = IE_ON_FATAL_ERR | IE_ON_PHYRDY_CHG |
  135. IE_ON_SIGNATURE_UPDATE |
  136. IE_ON_SINGL_DEVICE_ERR | IE_ON_CMD_COMPLETE,
  137. EXT_INDIRECT_SEG_PRD_FLAG = (1 << 31),
  138. DATA_SNOOP_ENABLE = (1 << 22),
  139. };
  140. /*
  141. * SATA Superset Registers
  142. */
  143. enum {
  144. SSTATUS = 0,
  145. SERROR = 4,
  146. SCONTROL = 8,
  147. SNOTIFY = 0xC,
  148. };
  149. /*
  150. * Control Status Register Set
  151. */
  152. enum {
  153. TRANSCFG = 0,
  154. TRANSSTATUS = 4,
  155. LINKCFG = 8,
  156. LINKCFG1 = 0xC,
  157. LINKCFG2 = 0x10,
  158. LINKSTATUS = 0x14,
  159. LINKSTATUS1 = 0x18,
  160. PHYCTRLCFG = 0x1C,
  161. COMMANDSTAT = 0x20,
  162. };
  163. /* PHY (link-layer) configuration control */
  164. enum {
  165. PHY_BIST_ENABLE = 0x01,
  166. };
  167. /*
  168. * Command Header Table entry, i.e, command slot
  169. * 4 Dwords per command slot, command header size == 64 Dwords.
  170. */
  171. struct cmdhdr_tbl_entry {
  172. u32 cda;
  173. u32 prde_fis_len;
  174. u32 ttl;
  175. u32 desc_info;
  176. };
  177. /*
  178. * Description information bitdefs
  179. */
  180. enum {
  181. VENDOR_SPECIFIC_BIST = (1 << 10),
  182. CMD_DESC_SNOOP_ENABLE = (1 << 9),
  183. FPDMA_QUEUED_CMD = (1 << 8),
  184. SRST_CMD = (1 << 7),
  185. BIST = (1 << 6),
  186. ATAPI_CMD = (1 << 5),
  187. };
  188. /*
  189. * Command Descriptor
  190. */
  191. struct command_desc {
  192. u8 cfis[8 * 4];
  193. u8 sfis[8 * 4];
  194. u8 acmd[4 * 4];
  195. u8 fill[4 * 4];
  196. u32 prdt[SATA_FSL_MAX_PRD_DIRECT * 4];
  197. u32 prdt_indirect[(SATA_FSL_MAX_PRD - SATA_FSL_MAX_PRD_DIRECT) * 4];
  198. };
  199. /*
  200. * Physical region table descriptor(PRD)
  201. */
  202. struct prde {
  203. u32 dba;
  204. u8 fill[2 * 4];
  205. u32 ddc_and_ext;
  206. };
  207. /*
  208. * ata_port private data
  209. * This is our per-port instance data.
  210. */
  211. struct sata_fsl_port_priv {
  212. struct cmdhdr_tbl_entry *cmdslot;
  213. dma_addr_t cmdslot_paddr;
  214. struct command_desc *cmdentry;
  215. dma_addr_t cmdentry_paddr;
  216. /*
  217. * SATA FSL controller has a Status FIS which should contain the
  218. * received D2H FIS & taskfile registers. This SFIS is present in
  219. * the command descriptor, and to have a ready reference to it,
  220. * we are caching it here, quite similar to what is done in H/W on
  221. * AHCI compliant devices by copying taskfile fields to a 32-bit
  222. * register.
  223. */
  224. struct ata_taskfile tf;
  225. };
  226. /*
  227. * ata_port->host_set private data
  228. */
  229. struct sata_fsl_host_priv {
  230. void __iomem *hcr_base;
  231. void __iomem *ssr_base;
  232. void __iomem *csr_base;
  233. int irq;
  234. };
  235. static inline unsigned int sata_fsl_tag(unsigned int tag,
  236. void __iomem *hcr_base)
  237. {
  238. /* We let libATA core do actual (queue) tag allocation */
  239. /* all non NCQ/queued commands should have tag#0 */
  240. if (ata_tag_internal(tag)) {
  241. DPRINTK("mapping internal cmds to tag#0\n");
  242. return 0;
  243. }
  244. if (unlikely(tag >= SATA_FSL_QUEUE_DEPTH)) {
  245. DPRINTK("tag %d invalid : out of range\n", tag);
  246. return 0;
  247. }
  248. if (unlikely((ioread32(hcr_base + CQ)) & (1 << tag))) {
  249. DPRINTK("tag %d invalid : in use!!\n", tag);
  250. return 0;
  251. }
  252. return tag;
  253. }
  254. static void sata_fsl_setup_cmd_hdr_entry(struct sata_fsl_port_priv *pp,
  255. unsigned int tag, u32 desc_info,
  256. u32 data_xfer_len, u8 num_prde,
  257. u8 fis_len)
  258. {
  259. dma_addr_t cmd_descriptor_address;
  260. cmd_descriptor_address = pp->cmdentry_paddr +
  261. tag * SATA_FSL_CMD_DESC_SIZE;
  262. /* NOTE: both data_xfer_len & fis_len are Dword counts */
  263. pp->cmdslot[tag].cda = cpu_to_le32(cmd_descriptor_address);
  264. pp->cmdslot[tag].prde_fis_len =
  265. cpu_to_le32((num_prde << 16) | (fis_len << 2));
  266. pp->cmdslot[tag].ttl = cpu_to_le32(data_xfer_len & ~0x03);
  267. pp->cmdslot[tag].desc_info = cpu_to_le32(desc_info | (tag & 0x1F));
  268. VPRINTK("cda=0x%x, prde_fis_len=0x%x, ttl=0x%x, di=0x%x\n",
  269. pp->cmdslot[tag].cda,
  270. pp->cmdslot[tag].prde_fis_len,
  271. pp->cmdslot[tag].ttl, pp->cmdslot[tag].desc_info);
  272. }
  273. static unsigned int sata_fsl_fill_sg(struct ata_queued_cmd *qc, void *cmd_desc,
  274. u32 *ttl, dma_addr_t cmd_desc_paddr)
  275. {
  276. struct scatterlist *sg;
  277. unsigned int num_prde = 0;
  278. u32 ttl_dwords = 0;
  279. /*
  280. * NOTE : direct & indirect prdt's are contigiously allocated
  281. */
  282. struct prde *prd = (struct prde *)&((struct command_desc *)
  283. cmd_desc)->prdt;
  284. struct prde *prd_ptr_to_indirect_ext = NULL;
  285. unsigned indirect_ext_segment_sz = 0;
  286. dma_addr_t indirect_ext_segment_paddr;
  287. unsigned int si;
  288. VPRINTK("SATA FSL : cd = 0x%x, prd = 0x%x\n", cmd_desc, prd);
  289. indirect_ext_segment_paddr = cmd_desc_paddr +
  290. SATA_FSL_CMD_DESC_OFFSET_TO_PRDT + SATA_FSL_MAX_PRD_DIRECT * 16;
  291. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  292. dma_addr_t sg_addr = sg_dma_address(sg);
  293. u32 sg_len = sg_dma_len(sg);
  294. VPRINTK("SATA FSL : fill_sg, sg_addr = 0x%x, sg_len = %d\n",
  295. sg_addr, sg_len);
  296. /* warn if each s/g element is not dword aligned */
  297. if (sg_addr & 0x03)
  298. ata_port_printk(qc->ap, KERN_ERR,
  299. "s/g addr unaligned : 0x%x\n", sg_addr);
  300. if (sg_len & 0x03)
  301. ata_port_printk(qc->ap, KERN_ERR,
  302. "s/g len unaligned : 0x%x\n", sg_len);
  303. if (num_prde == (SATA_FSL_MAX_PRD_DIRECT - 1) &&
  304. sg_next(sg) != NULL) {
  305. VPRINTK("setting indirect prde\n");
  306. prd_ptr_to_indirect_ext = prd;
  307. prd->dba = cpu_to_le32(indirect_ext_segment_paddr);
  308. indirect_ext_segment_sz = 0;
  309. ++prd;
  310. ++num_prde;
  311. }
  312. ttl_dwords += sg_len;
  313. prd->dba = cpu_to_le32(sg_addr);
  314. prd->ddc_and_ext =
  315. cpu_to_le32(DATA_SNOOP_ENABLE | (sg_len & ~0x03));
  316. VPRINTK("sg_fill, ttl=%d, dba=0x%x, ddc=0x%x\n",
  317. ttl_dwords, prd->dba, prd->ddc_and_ext);
  318. ++num_prde;
  319. ++prd;
  320. if (prd_ptr_to_indirect_ext)
  321. indirect_ext_segment_sz += sg_len;
  322. }
  323. if (prd_ptr_to_indirect_ext) {
  324. /* set indirect extension flag along with indirect ext. size */
  325. prd_ptr_to_indirect_ext->ddc_and_ext =
  326. cpu_to_le32((EXT_INDIRECT_SEG_PRD_FLAG |
  327. DATA_SNOOP_ENABLE |
  328. (indirect_ext_segment_sz & ~0x03)));
  329. }
  330. *ttl = ttl_dwords;
  331. return num_prde;
  332. }
  333. static void sata_fsl_qc_prep(struct ata_queued_cmd *qc)
  334. {
  335. struct ata_port *ap = qc->ap;
  336. struct sata_fsl_port_priv *pp = ap->private_data;
  337. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  338. void __iomem *hcr_base = host_priv->hcr_base;
  339. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  340. struct command_desc *cd;
  341. u32 desc_info = CMD_DESC_SNOOP_ENABLE;
  342. u32 num_prde = 0;
  343. u32 ttl_dwords = 0;
  344. dma_addr_t cd_paddr;
  345. cd = (struct command_desc *)pp->cmdentry + tag;
  346. cd_paddr = pp->cmdentry_paddr + tag * SATA_FSL_CMD_DESC_SIZE;
  347. ata_tf_to_fis(&qc->tf, 0, 1, (u8 *) &cd->cfis);
  348. VPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x\n",
  349. cd->cfis[0], cd->cfis[1], cd->cfis[2]);
  350. if (qc->tf.protocol == ATA_PROT_NCQ) {
  351. VPRINTK("FPDMA xfer,Sctor cnt[0:7],[8:15] = %d,%d\n",
  352. cd->cfis[3], cd->cfis[11]);
  353. }
  354. /* setup "ACMD - atapi command" in cmd. desc. if this is ATAPI cmd */
  355. if (ata_is_atapi(qc->tf.protocol)) {
  356. desc_info |= ATAPI_CMD;
  357. memset((void *)&cd->acmd, 0, 32);
  358. memcpy((void *)&cd->acmd, qc->cdb, qc->dev->cdb_len);
  359. }
  360. if (qc->flags & ATA_QCFLAG_DMAMAP)
  361. num_prde = sata_fsl_fill_sg(qc, (void *)cd,
  362. &ttl_dwords, cd_paddr);
  363. if (qc->tf.protocol == ATA_PROT_NCQ)
  364. desc_info |= FPDMA_QUEUED_CMD;
  365. sata_fsl_setup_cmd_hdr_entry(pp, tag, desc_info, ttl_dwords,
  366. num_prde, 5);
  367. VPRINTK("SATA FSL : xx_qc_prep, di = 0x%x, ttl = %d, num_prde = %d\n",
  368. desc_info, ttl_dwords, num_prde);
  369. }
  370. static unsigned int sata_fsl_qc_issue(struct ata_queued_cmd *qc)
  371. {
  372. struct ata_port *ap = qc->ap;
  373. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  374. void __iomem *hcr_base = host_priv->hcr_base;
  375. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  376. VPRINTK("xx_qc_issue called,CQ=0x%x,CA=0x%x,CE=0x%x,CC=0x%x\n",
  377. ioread32(CQ + hcr_base),
  378. ioread32(CA + hcr_base),
  379. ioread32(CE + hcr_base), ioread32(CC + hcr_base));
  380. /* Simply queue command to the controller/device */
  381. iowrite32(1 << tag, CQ + hcr_base);
  382. VPRINTK("xx_qc_issue called, tag=%d, CQ=0x%x, CA=0x%x\n",
  383. tag, ioread32(CQ + hcr_base), ioread32(CA + hcr_base));
  384. VPRINTK("CE=0x%x, DE=0x%x, CC=0x%x, CmdStat = 0x%x\n",
  385. ioread32(CE + hcr_base),
  386. ioread32(DE + hcr_base),
  387. ioread32(CC + hcr_base), ioread32(COMMANDSTAT + csr_base));
  388. return 0;
  389. }
  390. static int sata_fsl_scr_write(struct ata_port *ap, unsigned int sc_reg_in,
  391. u32 val)
  392. {
  393. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  394. void __iomem *ssr_base = host_priv->ssr_base;
  395. unsigned int sc_reg;
  396. switch (sc_reg_in) {
  397. case SCR_STATUS:
  398. case SCR_ERROR:
  399. case SCR_CONTROL:
  400. case SCR_ACTIVE:
  401. sc_reg = sc_reg_in;
  402. break;
  403. default:
  404. return -EINVAL;
  405. }
  406. VPRINTK("xx_scr_write, reg_in = %d\n", sc_reg);
  407. iowrite32(val, ssr_base + (sc_reg * 4));
  408. return 0;
  409. }
  410. static int sata_fsl_scr_read(struct ata_port *ap, unsigned int sc_reg_in,
  411. u32 *val)
  412. {
  413. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  414. void __iomem *ssr_base = host_priv->ssr_base;
  415. unsigned int sc_reg;
  416. switch (sc_reg_in) {
  417. case SCR_STATUS:
  418. case SCR_ERROR:
  419. case SCR_CONTROL:
  420. case SCR_ACTIVE:
  421. sc_reg = sc_reg_in;
  422. break;
  423. default:
  424. return -EINVAL;
  425. }
  426. VPRINTK("xx_scr_read, reg_in = %d\n", sc_reg);
  427. *val = ioread32(ssr_base + (sc_reg * 4));
  428. return 0;
  429. }
  430. static void sata_fsl_freeze(struct ata_port *ap)
  431. {
  432. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  433. void __iomem *hcr_base = host_priv->hcr_base;
  434. u32 temp;
  435. VPRINTK("xx_freeze, CQ=0x%x, CA=0x%x, CE=0x%x, DE=0x%x\n",
  436. ioread32(CQ + hcr_base),
  437. ioread32(CA + hcr_base),
  438. ioread32(CE + hcr_base), ioread32(DE + hcr_base));
  439. VPRINTK("CmdStat = 0x%x\n", ioread32(csr_base + COMMANDSTAT));
  440. /* disable interrupts on the controller/port */
  441. temp = ioread32(hcr_base + HCONTROL);
  442. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  443. VPRINTK("in xx_freeze : HControl = 0x%x, HStatus = 0x%x\n",
  444. ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
  445. }
  446. static void sata_fsl_thaw(struct ata_port *ap)
  447. {
  448. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  449. void __iomem *hcr_base = host_priv->hcr_base;
  450. u32 temp;
  451. /* ack. any pending IRQs for this controller/port */
  452. temp = ioread32(hcr_base + HSTATUS);
  453. VPRINTK("xx_thaw, pending IRQs = 0x%x\n", (temp & 0x3F));
  454. if (temp & 0x3F)
  455. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  456. /* enable interrupts on the controller/port */
  457. temp = ioread32(hcr_base + HCONTROL);
  458. iowrite32((temp | DEFAULT_PORT_IRQ_ENABLE_MASK), hcr_base + HCONTROL);
  459. VPRINTK("xx_thaw : HControl = 0x%x, HStatus = 0x%x\n",
  460. ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
  461. }
  462. /*
  463. * NOTE : 1st D2H FIS from device does not update sfis in command descriptor.
  464. */
  465. static inline void sata_fsl_cache_taskfile_from_d2h_fis(struct ata_queued_cmd
  466. *qc,
  467. struct ata_port *ap)
  468. {
  469. struct sata_fsl_port_priv *pp = ap->private_data;
  470. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  471. void __iomem *hcr_base = host_priv->hcr_base;
  472. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  473. struct command_desc *cd;
  474. cd = pp->cmdentry + tag;
  475. ata_tf_from_fis(cd->sfis, &pp->tf);
  476. }
  477. static u8 sata_fsl_check_status(struct ata_port *ap)
  478. {
  479. struct sata_fsl_port_priv *pp = ap->private_data;
  480. return pp->tf.command;
  481. }
  482. static void sata_fsl_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  483. {
  484. struct sata_fsl_port_priv *pp = ap->private_data;
  485. *tf = pp->tf;
  486. }
  487. static int sata_fsl_port_start(struct ata_port *ap)
  488. {
  489. struct device *dev = ap->host->dev;
  490. struct sata_fsl_port_priv *pp;
  491. int retval;
  492. void *mem;
  493. dma_addr_t mem_dma;
  494. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  495. void __iomem *hcr_base = host_priv->hcr_base;
  496. u32 temp;
  497. pp = kzalloc(sizeof(*pp), GFP_KERNEL);
  498. if (!pp)
  499. return -ENOMEM;
  500. mem = dma_alloc_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ, &mem_dma,
  501. GFP_KERNEL);
  502. if (!mem) {
  503. kfree(pp);
  504. return -ENOMEM;
  505. }
  506. memset(mem, 0, SATA_FSL_PORT_PRIV_DMA_SZ);
  507. pp->cmdslot = mem;
  508. pp->cmdslot_paddr = mem_dma;
  509. mem += SATA_FSL_CMD_SLOT_SIZE;
  510. mem_dma += SATA_FSL_CMD_SLOT_SIZE;
  511. pp->cmdentry = mem;
  512. pp->cmdentry_paddr = mem_dma;
  513. ap->private_data = pp;
  514. VPRINTK("CHBA = 0x%x, cmdentry_phys = 0x%x\n",
  515. pp->cmdslot_paddr, pp->cmdentry_paddr);
  516. /* Now, update the CHBA register in host controller cmd register set */
  517. iowrite32(pp->cmdslot_paddr & 0xffffffff, hcr_base + CHBA);
  518. /*
  519. * Now, we can bring the controller on-line & also initiate
  520. * the COMINIT sequence, we simply return here and the boot-probing
  521. * & device discovery process is re-initiated by libATA using a
  522. * Softreset EH (dummy) session. Hence, boot probing and device
  523. * discovey will be part of sata_fsl_softreset() callback.
  524. */
  525. temp = ioread32(hcr_base + HCONTROL);
  526. iowrite32((temp | HCONTROL_ONLINE_PHY_RST), hcr_base + HCONTROL);
  527. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  528. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  529. VPRINTK("CHBA = 0x%x\n", ioread32(hcr_base + CHBA));
  530. #ifdef CONFIG_MPC8315_DS
  531. /*
  532. * Workaround for 8315DS board 3gbps link-up issue,
  533. * currently limit SATA port to GEN1 speed
  534. */
  535. sata_fsl_scr_read(ap, SCR_CONTROL, &temp);
  536. temp &= ~(0xF << 4);
  537. temp |= (0x1 << 4);
  538. sata_fsl_scr_write(ap, SCR_CONTROL, temp);
  539. sata_fsl_scr_read(ap, SCR_CONTROL, &temp);
  540. dev_printk(KERN_WARNING, dev, "scr_control, speed limited to %x\n",
  541. temp);
  542. #endif
  543. return 0;
  544. }
  545. static void sata_fsl_port_stop(struct ata_port *ap)
  546. {
  547. struct device *dev = ap->host->dev;
  548. struct sata_fsl_port_priv *pp = ap->private_data;
  549. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  550. void __iomem *hcr_base = host_priv->hcr_base;
  551. u32 temp;
  552. /*
  553. * Force host controller to go off-line, aborting current operations
  554. */
  555. temp = ioread32(hcr_base + HCONTROL);
  556. temp &= ~HCONTROL_ONLINE_PHY_RST;
  557. temp |= HCONTROL_FORCE_OFFLINE;
  558. iowrite32(temp, hcr_base + HCONTROL);
  559. /* Poll for controller to go offline - should happen immediately */
  560. ata_wait_register(hcr_base + HSTATUS, ONLINE, ONLINE, 1, 1);
  561. ap->private_data = NULL;
  562. dma_free_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ,
  563. pp->cmdslot, pp->cmdslot_paddr);
  564. kfree(pp);
  565. }
  566. static unsigned int sata_fsl_dev_classify(struct ata_port *ap)
  567. {
  568. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  569. void __iomem *hcr_base = host_priv->hcr_base;
  570. struct ata_taskfile tf;
  571. u32 temp;
  572. temp = ioread32(hcr_base + SIGNATURE);
  573. VPRINTK("raw sig = 0x%x\n", temp);
  574. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  575. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  576. tf.lbah = (temp >> 24) & 0xff;
  577. tf.lbam = (temp >> 16) & 0xff;
  578. tf.lbal = (temp >> 8) & 0xff;
  579. tf.nsect = temp & 0xff;
  580. return ata_dev_classify(&tf);
  581. }
  582. static int sata_fsl_softreset(struct ata_link *link, unsigned int *class,
  583. unsigned long deadline)
  584. {
  585. struct ata_port *ap = link->ap;
  586. struct sata_fsl_port_priv *pp = ap->private_data;
  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. struct ata_taskfile tf;
  591. u8 *cfis;
  592. u32 Serror;
  593. int i = 0;
  594. unsigned long start_jiffies;
  595. DPRINTK("in xx_softreset\n");
  596. try_offline_again:
  597. /*
  598. * Force host controller to go off-line, aborting current operations
  599. */
  600. temp = ioread32(hcr_base + HCONTROL);
  601. temp &= ~HCONTROL_ONLINE_PHY_RST;
  602. iowrite32(temp, hcr_base + HCONTROL);
  603. /* Poll for controller to go offline */
  604. temp = ata_wait_register(hcr_base + HSTATUS, ONLINE, ONLINE, 1, 500);
  605. if (temp & ONLINE) {
  606. ata_port_printk(ap, KERN_ERR,
  607. "Softreset failed, not off-lined %d\n", i);
  608. /*
  609. * Try to offline controller atleast twice
  610. */
  611. i++;
  612. if (i == 2)
  613. goto err;
  614. else
  615. goto try_offline_again;
  616. }
  617. DPRINTK("softreset, controller off-lined\n");
  618. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  619. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  620. /*
  621. * PHY reset should remain asserted for atleast 1ms
  622. */
  623. msleep(1);
  624. /*
  625. * Now, bring the host controller online again, this can take time
  626. * as PHY reset and communication establishment, 1st D2H FIS and
  627. * device signature update is done, on safe side assume 500ms
  628. * NOTE : Host online status may be indicated immediately!!
  629. */
  630. temp = ioread32(hcr_base + HCONTROL);
  631. temp |= (HCONTROL_ONLINE_PHY_RST | HCONTROL_SNOOP_ENABLE);
  632. iowrite32(temp, hcr_base + HCONTROL);
  633. temp = ata_wait_register(hcr_base + HSTATUS, ONLINE, 0, 1, 500);
  634. if (!(temp & ONLINE)) {
  635. ata_port_printk(ap, KERN_ERR,
  636. "Softreset failed, not on-lined\n");
  637. goto err;
  638. }
  639. DPRINTK("softreset, controller off-lined & on-lined\n");
  640. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  641. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  642. /*
  643. * First, wait for the PHYRDY change to occur before waiting for
  644. * the signature, and also verify if SStatus indicates device
  645. * presence
  646. */
  647. temp = ata_wait_register(hcr_base + HSTATUS, 0xFF, 0, 1, 500);
  648. if ((!(temp & 0x10)) || ata_link_offline(link)) {
  649. ata_port_printk(ap, KERN_WARNING,
  650. "No Device OR PHYRDY change,Hstatus = 0x%x\n",
  651. ioread32(hcr_base + HSTATUS));
  652. goto err;
  653. }
  654. /*
  655. * Wait for the first D2H from device,i.e,signature update notification
  656. */
  657. start_jiffies = jiffies;
  658. temp = ata_wait_register(hcr_base + HSTATUS, 0xFF, 0x10,
  659. 500, jiffies_to_msecs(deadline - start_jiffies));
  660. if ((temp & 0xFF) != 0x18) {
  661. ata_port_printk(ap, KERN_WARNING, "No Signature Update\n");
  662. goto err;
  663. } else {
  664. ata_port_printk(ap, KERN_INFO,
  665. "Signature Update detected @ %d msecs\n",
  666. jiffies_to_msecs(jiffies - start_jiffies));
  667. }
  668. /*
  669. * Send a device reset (SRST) explicitly on command slot #0
  670. * Check : will the command queue (reg) be cleared during offlining ??
  671. * Also we will be online only if Phy commn. has been established
  672. * and device presence has been detected, therefore if we have
  673. * reached here, we can send a command to the target device
  674. */
  675. DPRINTK("Sending SRST/device reset\n");
  676. ata_tf_init(link->device, &tf);
  677. cfis = (u8 *) &pp->cmdentry->cfis;
  678. /* device reset/SRST is a control register update FIS, uses tag0 */
  679. sata_fsl_setup_cmd_hdr_entry(pp, 0,
  680. SRST_CMD | CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
  681. tf.ctl |= ATA_SRST; /* setup SRST bit in taskfile control reg */
  682. ata_tf_to_fis(&tf, 0, 0, cfis);
  683. DPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x, 0x%x\n",
  684. cfis[0], cfis[1], cfis[2], cfis[3]);
  685. /*
  686. * Queue SRST command to the controller/device, ensure that no
  687. * other commands are active on the controller/device
  688. */
  689. DPRINTK("@Softreset, CQ = 0x%x, CA = 0x%x, CC = 0x%x\n",
  690. ioread32(CQ + hcr_base),
  691. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  692. iowrite32(0xFFFF, CC + hcr_base);
  693. iowrite32(1, CQ + hcr_base);
  694. temp = ata_wait_register(CQ + hcr_base, 0x1, 0x1, 1, 5000);
  695. if (temp & 0x1) {
  696. ata_port_printk(ap, KERN_WARNING, "ATA_SRST issue failed\n");
  697. DPRINTK("Softreset@5000,CQ=0x%x,CA=0x%x,CC=0x%x\n",
  698. ioread32(CQ + hcr_base),
  699. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  700. sata_fsl_scr_read(ap, SCR_ERROR, &Serror);
  701. DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  702. DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  703. DPRINTK("Serror = 0x%x\n", Serror);
  704. goto err;
  705. }
  706. msleep(1);
  707. /*
  708. * SATA device enters reset state after receving a Control register
  709. * FIS with SRST bit asserted and it awaits another H2D Control reg.
  710. * FIS with SRST bit cleared, then the device does internal diags &
  711. * initialization, followed by indicating it's initialization status
  712. * using ATA signature D2H register FIS to the host controller.
  713. */
  714. sata_fsl_setup_cmd_hdr_entry(pp, 0, CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
  715. tf.ctl &= ~ATA_SRST; /* 2nd H2D Ctl. register FIS */
  716. ata_tf_to_fis(&tf, 0, 0, cfis);
  717. iowrite32(1, CQ + hcr_base);
  718. msleep(150); /* ?? */
  719. /*
  720. * The above command would have signalled an interrupt on command
  721. * complete, which needs special handling, by clearing the Nth
  722. * command bit of the CCreg
  723. */
  724. iowrite32(0x01, CC + hcr_base); /* We know it will be cmd#0 always */
  725. DPRINTK("SATA FSL : Now checking device signature\n");
  726. *class = ATA_DEV_NONE;
  727. /* Verify if SStatus indicates device presence */
  728. if (ata_link_online(link)) {
  729. /*
  730. * if we are here, device presence has been detected,
  731. * 1st D2H FIS would have been received, but sfis in
  732. * command desc. is not updated, but signature register
  733. * would have been updated
  734. */
  735. *class = sata_fsl_dev_classify(ap);
  736. DPRINTK("class = %d\n", *class);
  737. VPRINTK("ccreg = 0x%x\n", ioread32(hcr_base + CC));
  738. VPRINTK("cereg = 0x%x\n", ioread32(hcr_base + CE));
  739. }
  740. return 0;
  741. err:
  742. return -EIO;
  743. }
  744. static void sata_fsl_error_handler(struct ata_port *ap)
  745. {
  746. DPRINTK("in xx_error_handler\n");
  747. /* perform recovery */
  748. ata_do_eh(ap, ata_std_prereset, sata_fsl_softreset, sata_std_hardreset,
  749. ata_std_postreset);
  750. }
  751. static void sata_fsl_post_internal_cmd(struct ata_queued_cmd *qc)
  752. {
  753. if (qc->flags & ATA_QCFLAG_FAILED)
  754. qc->err_mask |= AC_ERR_OTHER;
  755. if (qc->err_mask) {
  756. /* make DMA engine forget about the failed command */
  757. }
  758. }
  759. static void sata_fsl_irq_clear(struct ata_port *ap)
  760. {
  761. /* unused */
  762. }
  763. static void sata_fsl_error_intr(struct ata_port *ap)
  764. {
  765. struct ata_link *link = &ap->link;
  766. struct ata_eh_info *ehi = &link->eh_info;
  767. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  768. void __iomem *hcr_base = host_priv->hcr_base;
  769. u32 hstatus, dereg, cereg = 0, SError = 0;
  770. unsigned int err_mask = 0, action = 0;
  771. struct ata_queued_cmd *qc;
  772. int freeze = 0;
  773. hstatus = ioread32(hcr_base + HSTATUS);
  774. cereg = ioread32(hcr_base + CE);
  775. ata_ehi_clear_desc(ehi);
  776. /*
  777. * Handle & Clear SError
  778. */
  779. sata_fsl_scr_read(ap, SCR_ERROR, &SError);
  780. if (unlikely(SError & 0xFFFF0000)) {
  781. sata_fsl_scr_write(ap, SCR_ERROR, SError);
  782. err_mask |= AC_ERR_ATA_BUS;
  783. }
  784. DPRINTK("error_intr,hStat=0x%x,CE=0x%x,DE =0x%x,SErr=0x%x\n",
  785. hstatus, cereg, ioread32(hcr_base + DE), SError);
  786. /* handle single device errors */
  787. if (cereg) {
  788. /*
  789. * clear the command error, also clears queue to the device
  790. * in error, and we can (re)issue commands to this device.
  791. * When a device is in error all commands queued into the
  792. * host controller and at the device are considered aborted
  793. * and the queue for that device is stopped. Now, after
  794. * clearing the device error, we can issue commands to the
  795. * device to interrogate it to find the source of the error.
  796. */
  797. dereg = ioread32(hcr_base + DE);
  798. iowrite32(dereg, hcr_base + DE);
  799. iowrite32(cereg, hcr_base + CE);
  800. DPRINTK("single device error, CE=0x%x, DE=0x%x\n",
  801. ioread32(hcr_base + CE), ioread32(hcr_base + DE));
  802. /*
  803. * We should consider this as non fatal error, and TF must
  804. * be updated as done below.
  805. */
  806. err_mask |= AC_ERR_DEV;
  807. }
  808. /* handle fatal errors */
  809. if (hstatus & FATAL_ERROR_DECODE) {
  810. err_mask |= AC_ERR_ATA_BUS;
  811. action |= ATA_EH_SOFTRESET;
  812. /* how will fatal error interrupts be completed ?? */
  813. freeze = 1;
  814. }
  815. /* Handle PHYRDY change notification */
  816. if (hstatus & INT_ON_PHYRDY_CHG) {
  817. DPRINTK("SATA FSL: PHYRDY change indication\n");
  818. /* Setup a soft-reset EH action */
  819. ata_ehi_hotplugged(ehi);
  820. freeze = 1;
  821. }
  822. /* record error info */
  823. qc = ata_qc_from_tag(ap, link->active_tag);
  824. if (qc) {
  825. sata_fsl_cache_taskfile_from_d2h_fis(qc, qc->ap);
  826. qc->err_mask |= err_mask;
  827. } else
  828. ehi->err_mask |= err_mask;
  829. ehi->action |= action;
  830. ehi->serror |= SError;
  831. /* freeze or abort */
  832. if (freeze)
  833. ata_port_freeze(ap);
  834. else
  835. ata_port_abort(ap);
  836. }
  837. static void sata_fsl_qc_complete(struct ata_queued_cmd *qc)
  838. {
  839. if (qc->flags & ATA_QCFLAG_RESULT_TF) {
  840. DPRINTK("xx_qc_complete called\n");
  841. sata_fsl_cache_taskfile_from_d2h_fis(qc, qc->ap);
  842. }
  843. }
  844. static void sata_fsl_host_intr(struct ata_port *ap)
  845. {
  846. struct ata_link *link = &ap->link;
  847. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  848. void __iomem *hcr_base = host_priv->hcr_base;
  849. u32 hstatus, qc_active = 0;
  850. struct ata_queued_cmd *qc;
  851. u32 SError;
  852. hstatus = ioread32(hcr_base + HSTATUS);
  853. sata_fsl_scr_read(ap, SCR_ERROR, &SError);
  854. if (unlikely(SError & 0xFFFF0000)) {
  855. DPRINTK("serror @host_intr : 0x%x\n", SError);
  856. sata_fsl_error_intr(ap);
  857. }
  858. if (unlikely(hstatus & INT_ON_ERROR)) {
  859. DPRINTK("error interrupt!!\n");
  860. sata_fsl_error_intr(ap);
  861. return;
  862. }
  863. if (link->sactive) { /* only true for NCQ commands */
  864. int i;
  865. /* Read command completed register */
  866. qc_active = ioread32(hcr_base + CC);
  867. /* clear CC bit, this will also complete the interrupt */
  868. iowrite32(qc_active, hcr_base + CC);
  869. DPRINTK("Status of all queues :\n");
  870. DPRINTK("qc_active/CC = 0x%x, CA = 0x%x, CE=0x%x\n",
  871. qc_active, ioread32(hcr_base + CA),
  872. ioread32(hcr_base + CE));
  873. for (i = 0; i < SATA_FSL_QUEUE_DEPTH; i++) {
  874. if (qc_active & (1 << i)) {
  875. qc = ata_qc_from_tag(ap, i);
  876. if (qc) {
  877. sata_fsl_qc_complete(qc);
  878. ata_qc_complete(qc);
  879. }
  880. DPRINTK
  881. ("completing ncq cmd,tag=%d,CC=0x%x,CA=0x%x\n",
  882. i, ioread32(hcr_base + CC),
  883. ioread32(hcr_base + CA));
  884. }
  885. }
  886. return;
  887. } else if (ap->qc_active) {
  888. iowrite32(1, hcr_base + CC);
  889. qc = ata_qc_from_tag(ap, link->active_tag);
  890. DPRINTK("completing non-ncq cmd, tag=%d,CC=0x%x\n",
  891. link->active_tag, ioread32(hcr_base + CC));
  892. if (qc) {
  893. sata_fsl_qc_complete(qc);
  894. ata_qc_complete(qc);
  895. }
  896. } else {
  897. /* Spurious Interrupt!! */
  898. DPRINTK("spurious interrupt!!, CC = 0x%x\n",
  899. ioread32(hcr_base + CC));
  900. return;
  901. }
  902. }
  903. static irqreturn_t sata_fsl_interrupt(int irq, void *dev_instance)
  904. {
  905. struct ata_host *host = dev_instance;
  906. struct sata_fsl_host_priv *host_priv = host->private_data;
  907. void __iomem *hcr_base = host_priv->hcr_base;
  908. u32 interrupt_enables;
  909. unsigned handled = 0;
  910. struct ata_port *ap;
  911. /* ack. any pending IRQs for this controller/port */
  912. interrupt_enables = ioread32(hcr_base + HSTATUS);
  913. interrupt_enables &= 0x3F;
  914. DPRINTK("interrupt status 0x%x\n", interrupt_enables);
  915. if (!interrupt_enables)
  916. return IRQ_NONE;
  917. spin_lock(&host->lock);
  918. /* Assuming one port per host controller */
  919. ap = host->ports[0];
  920. if (ap) {
  921. sata_fsl_host_intr(ap);
  922. } else {
  923. dev_printk(KERN_WARNING, host->dev,
  924. "interrupt on disabled port 0\n");
  925. }
  926. iowrite32(interrupt_enables, hcr_base + HSTATUS);
  927. handled = 1;
  928. spin_unlock(&host->lock);
  929. return IRQ_RETVAL(handled);
  930. }
  931. /*
  932. * Multiple ports are represented by multiple SATA controllers with
  933. * one port per controller
  934. */
  935. static int sata_fsl_init_controller(struct ata_host *host)
  936. {
  937. struct sata_fsl_host_priv *host_priv = host->private_data;
  938. void __iomem *hcr_base = host_priv->hcr_base;
  939. u32 temp;
  940. /*
  941. * NOTE : We cannot bring the controller online before setting
  942. * the CHBA, hence main controller initialization is done as
  943. * part of the port_start() callback
  944. */
  945. /* ack. any pending IRQs for this controller/port */
  946. temp = ioread32(hcr_base + HSTATUS);
  947. if (temp & 0x3F)
  948. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  949. /* Keep interrupts disabled on the controller */
  950. temp = ioread32(hcr_base + HCONTROL);
  951. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  952. /* Disable interrupt coalescing control(icc), for the moment */
  953. DPRINTK("icc = 0x%x\n", ioread32(hcr_base + ICC));
  954. iowrite32(0x01000000, hcr_base + ICC);
  955. /* clear error registers, SError is cleared by libATA */
  956. iowrite32(0x00000FFFF, hcr_base + CE);
  957. iowrite32(0x00000FFFF, hcr_base + DE);
  958. /* initially assuming no Port multiplier, set CQPMP to 0 */
  959. iowrite32(0x0, hcr_base + CQPMP);
  960. /*
  961. * host controller will be brought on-line, during xx_port_start()
  962. * callback, that should also initiate the OOB, COMINIT sequence
  963. */
  964. DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  965. DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  966. return 0;
  967. }
  968. /*
  969. * scsi mid-layer and libata interface structures
  970. */
  971. static struct scsi_host_template sata_fsl_sht = {
  972. .module = THIS_MODULE,
  973. .name = "sata_fsl",
  974. .ioctl = ata_scsi_ioctl,
  975. .queuecommand = ata_scsi_queuecmd,
  976. .change_queue_depth = ata_scsi_change_queue_depth,
  977. .can_queue = SATA_FSL_QUEUE_DEPTH,
  978. .this_id = ATA_SHT_THIS_ID,
  979. .sg_tablesize = SATA_FSL_MAX_PRD_USABLE,
  980. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  981. .emulated = ATA_SHT_EMULATED,
  982. .use_clustering = ATA_SHT_USE_CLUSTERING,
  983. .proc_name = "sata_fsl",
  984. .dma_boundary = ATA_DMA_BOUNDARY,
  985. .slave_configure = ata_scsi_slave_config,
  986. .slave_destroy = ata_scsi_slave_destroy,
  987. .bios_param = ata_std_bios_param,
  988. };
  989. static const struct ata_port_operations sata_fsl_ops = {
  990. .check_status = sata_fsl_check_status,
  991. .check_altstatus = sata_fsl_check_status,
  992. .dev_select = ata_noop_dev_select,
  993. .tf_read = sata_fsl_tf_read,
  994. .qc_prep = sata_fsl_qc_prep,
  995. .qc_issue = sata_fsl_qc_issue,
  996. .irq_clear = sata_fsl_irq_clear,
  997. .scr_read = sata_fsl_scr_read,
  998. .scr_write = sata_fsl_scr_write,
  999. .freeze = sata_fsl_freeze,
  1000. .thaw = sata_fsl_thaw,
  1001. .error_handler = sata_fsl_error_handler,
  1002. .post_internal_cmd = sata_fsl_post_internal_cmd,
  1003. .port_start = sata_fsl_port_start,
  1004. .port_stop = sata_fsl_port_stop,
  1005. };
  1006. static const struct ata_port_info sata_fsl_port_info[] = {
  1007. {
  1008. .flags = SATA_FSL_HOST_FLAGS,
  1009. .link_flags = SATA_FSL_HOST_LFLAGS,
  1010. .pio_mask = 0x1f, /* pio 0-4 */
  1011. .udma_mask = 0x7f, /* udma 0-6 */
  1012. .port_ops = &sata_fsl_ops,
  1013. },
  1014. };
  1015. static int sata_fsl_probe(struct of_device *ofdev,
  1016. const struct of_device_id *match)
  1017. {
  1018. int retval = 0;
  1019. void __iomem *hcr_base = NULL;
  1020. void __iomem *ssr_base = NULL;
  1021. void __iomem *csr_base = NULL;
  1022. struct sata_fsl_host_priv *host_priv = NULL;
  1023. struct resource *r;
  1024. int irq;
  1025. struct ata_host *host;
  1026. struct ata_port_info pi = sata_fsl_port_info[0];
  1027. const struct ata_port_info *ppi[] = { &pi, NULL };
  1028. dev_printk(KERN_INFO, &ofdev->dev,
  1029. "Sata FSL Platform/CSB Driver init\n");
  1030. r = kmalloc(sizeof(struct resource), GFP_KERNEL);
  1031. hcr_base = of_iomap(ofdev->node, 0);
  1032. if (!hcr_base)
  1033. goto error_exit_with_cleanup;
  1034. ssr_base = hcr_base + 0x100;
  1035. csr_base = hcr_base + 0x140;
  1036. DPRINTK("@reset i/o = 0x%x\n", ioread32(csr_base + TRANSCFG));
  1037. DPRINTK("sizeof(cmd_desc) = %d\n", sizeof(struct command_desc));
  1038. DPRINTK("sizeof(#define cmd_desc) = %d\n", SATA_FSL_CMD_DESC_SIZE);
  1039. host_priv = kzalloc(sizeof(struct sata_fsl_host_priv), GFP_KERNEL);
  1040. if (!host_priv)
  1041. goto error_exit_with_cleanup;
  1042. host_priv->hcr_base = hcr_base;
  1043. host_priv->ssr_base = ssr_base;
  1044. host_priv->csr_base = csr_base;
  1045. irq = irq_of_parse_and_map(ofdev->node, 0);
  1046. if (irq < 0) {
  1047. dev_printk(KERN_ERR, &ofdev->dev, "invalid irq from platform\n");
  1048. goto error_exit_with_cleanup;
  1049. }
  1050. host_priv->irq = irq;
  1051. /* allocate host structure */
  1052. host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_FSL_MAX_PORTS);
  1053. /* host->iomap is not used currently */
  1054. host->private_data = host_priv;
  1055. /* setup port(s) */
  1056. host->ports[0]->ioaddr.cmd_addr = host_priv->hcr_base;
  1057. host->ports[0]->ioaddr.scr_addr = host_priv->ssr_base;
  1058. /* initialize host controller */
  1059. sata_fsl_init_controller(host);
  1060. /*
  1061. * Now, register with libATA core, this will also initiate the
  1062. * device discovery process, invoking our port_start() handler &
  1063. * error_handler() to execute a dummy Softreset EH session
  1064. */
  1065. ata_host_activate(host, irq, sata_fsl_interrupt, SATA_FSL_IRQ_FLAG,
  1066. &sata_fsl_sht);
  1067. dev_set_drvdata(&ofdev->dev, host);
  1068. return 0;
  1069. error_exit_with_cleanup:
  1070. if (hcr_base)
  1071. iounmap(hcr_base);
  1072. if (host_priv)
  1073. kfree(host_priv);
  1074. return retval;
  1075. }
  1076. static int sata_fsl_remove(struct of_device *ofdev)
  1077. {
  1078. struct ata_host *host = dev_get_drvdata(&ofdev->dev);
  1079. struct sata_fsl_host_priv *host_priv = host->private_data;
  1080. ata_host_detach(host);
  1081. dev_set_drvdata(&ofdev->dev, NULL);
  1082. irq_dispose_mapping(host_priv->irq);
  1083. iounmap(host_priv->hcr_base);
  1084. kfree(host_priv);
  1085. return 0;
  1086. }
  1087. static struct of_device_id fsl_sata_match[] = {
  1088. {
  1089. .compatible = "fsl,mpc8315-sata",
  1090. },
  1091. {
  1092. .compatible = "fsl,mpc8379-sata",
  1093. },
  1094. {},
  1095. };
  1096. MODULE_DEVICE_TABLE(of, fsl_sata_match);
  1097. static struct of_platform_driver fsl_sata_driver = {
  1098. .name = "fsl-sata",
  1099. .match_table = fsl_sata_match,
  1100. .probe = sata_fsl_probe,
  1101. .remove = sata_fsl_remove,
  1102. };
  1103. static int __init sata_fsl_init(void)
  1104. {
  1105. of_register_platform_driver(&fsl_sata_driver);
  1106. return 0;
  1107. }
  1108. static void __exit sata_fsl_exit(void)
  1109. {
  1110. of_unregister_platform_driver(&fsl_sata_driver);
  1111. }
  1112. MODULE_LICENSE("GPL");
  1113. MODULE_AUTHOR("Ashish Kalra, Freescale Semiconductor");
  1114. MODULE_DESCRIPTION("Freescale 3.0Gbps SATA controller low level driver");
  1115. MODULE_VERSION("1.10");
  1116. module_init(sata_fsl_init);
  1117. module_exit(sata_fsl_exit);