ahci-init-xlp2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright (c) 2003-2014 Broadcom Corporation
  3. * All Rights Reserved
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the Broadcom
  9. * license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY BROADCOM ``AS IS'' AND ANY EXPRESS OR
  23. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  29. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  31. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  32. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <linux/dma-mapping.h>
  35. #include <linux/kernel.h>
  36. #include <linux/delay.h>
  37. #include <linux/init.h>
  38. #include <linux/pci.h>
  39. #include <linux/irq.h>
  40. #include <linux/bitops.h>
  41. #include <linux/pci_ids.h>
  42. #include <linux/nodemask.h>
  43. #include <asm/cpu.h>
  44. #include <asm/mipsregs.h>
  45. #include <asm/netlogic/common.h>
  46. #include <asm/netlogic/haldefs.h>
  47. #include <asm/netlogic/mips-extns.h>
  48. #include <asm/netlogic/xlp-hal/xlp.h>
  49. #include <asm/netlogic/xlp-hal/iomap.h>
  50. #define SATA_CTL 0x0
  51. #define SATA_STATUS 0x1 /* Status Reg */
  52. #define SATA_INT 0x2 /* Interrupt Reg */
  53. #define SATA_INT_MASK 0x3 /* Interrupt Mask Reg */
  54. #define SATA_BIU_TIMEOUT 0x4
  55. #define AXIWRSPERRLOG 0x5
  56. #define AXIRDSPERRLOG 0x6
  57. #define BiuTimeoutLow 0x7
  58. #define BiuTimeoutHi 0x8
  59. #define BiuSlvErLow 0x9
  60. #define BiuSlvErHi 0xa
  61. #define IO_CONFIG_SWAP_DIS 0xb
  62. #define CR_REG_TIMER 0xc
  63. #define CORE_ID 0xd
  64. #define AXI_SLAVE_OPT1 0xe
  65. #define PHY_MEM_ACCESS 0xf
  66. #define PHY0_CNTRL 0x10
  67. #define PHY0_STAT 0x11
  68. #define PHY0_RX_ALIGN 0x12
  69. #define PHY0_RX_EQ_LO 0x13
  70. #define PHY0_RX_EQ_HI 0x14
  71. #define PHY0_BIST_LOOP 0x15
  72. #define PHY1_CNTRL 0x16
  73. #define PHY1_STAT 0x17
  74. #define PHY1_RX_ALIGN 0x18
  75. #define PHY1_RX_EQ_LO 0x19
  76. #define PHY1_RX_EQ_HI 0x1a
  77. #define PHY1_BIST_LOOP 0x1b
  78. #define RdExBase 0x1c
  79. #define RdExLimit 0x1d
  80. #define CacheAllocBase 0x1e
  81. #define CacheAllocLimit 0x1f
  82. #define BiuSlaveCmdGstNum 0x20
  83. /*SATA_CTL Bits */
  84. #define SATA_RST_N BIT(0) /* Active low reset sata_core phy */
  85. #define SataCtlReserve0 BIT(1)
  86. #define M_CSYSREQ BIT(2) /* AXI master low power, not used */
  87. #define S_CSYSREQ BIT(3) /* AXI slave low power, not used */
  88. #define P0_CP_DET BIT(8) /* Reserved, bring in from pad */
  89. #define P0_MP_SW BIT(9) /* Mech Switch */
  90. #define P0_DISABLE BIT(10) /* disable p0 */
  91. #define P0_ACT_LED_EN BIT(11) /* Active LED enable */
  92. #define P0_IRST_HARD_SYNTH BIT(12) /* PHY hard synth reset */
  93. #define P0_IRST_HARD_TXRX BIT(13) /* PHY lane hard reset */
  94. #define P0_IRST_POR BIT(14) /* PHY power on reset*/
  95. #define P0_IPDTXL BIT(15) /* PHY Tx lane dis/power down */
  96. #define P0_IPDRXL BIT(16) /* PHY Rx lane dis/power down */
  97. #define P0_IPDIPDMSYNTH BIT(17) /* PHY synthesizer dis/porwer down */
  98. #define P0_CP_POD_EN BIT(18) /* CP_POD enable */
  99. #define P0_AT_BYPASS BIT(19) /* P0 address translation by pass */
  100. #define P1_CP_DET BIT(20) /* Reserved,Cold Detect */
  101. #define P1_MP_SW BIT(21) /* Mech Switch */
  102. #define P1_DISABLE BIT(22) /* disable p1 */
  103. #define P1_ACT_LED_EN BIT(23) /* Active LED enable */
  104. #define P1_IRST_HARD_SYNTH BIT(24) /* PHY hard synth reset */
  105. #define P1_IRST_HARD_TXRX BIT(25) /* PHY lane hard reset */
  106. #define P1_IRST_POR BIT(26) /* PHY power on reset*/
  107. #define P1_IPDTXL BIT(27) /* PHY Tx lane dis/porwer down */
  108. #define P1_IPDRXL BIT(28) /* PHY Rx lane dis/porwer down */
  109. #define P1_IPDIPDMSYNTH BIT(29) /* PHY synthesizer dis/porwer down */
  110. #define P1_CP_POD_EN BIT(30)
  111. #define P1_AT_BYPASS BIT(31) /* P1 address translation by pass */
  112. /* Status register */
  113. #define M_CACTIVE BIT(0) /* m_cactive, not used */
  114. #define S_CACTIVE BIT(1) /* s_cactive, not used */
  115. #define P0_PHY_READY BIT(8) /* phy is ready */
  116. #define P0_CP_POD BIT(9) /* Cold PowerOn */
  117. #define P0_SLUMBER BIT(10) /* power mode slumber */
  118. #define P0_PATIAL BIT(11) /* power mode patial */
  119. #define P0_PHY_SIG_DET BIT(12) /* phy dignal detect */
  120. #define P0_PHY_CALI BIT(13) /* phy calibration done */
  121. #define P1_PHY_READY BIT(16) /* phy is ready */
  122. #define P1_CP_POD BIT(17) /* Cold PowerOn */
  123. #define P1_SLUMBER BIT(18) /* power mode slumber */
  124. #define P1_PATIAL BIT(19) /* power mode patial */
  125. #define P1_PHY_SIG_DET BIT(20) /* phy dignal detect */
  126. #define P1_PHY_CALI BIT(21) /* phy calibration done */
  127. /* SATA CR_REG_TIMER bits */
  128. #define CR_TIME_SCALE (0x1000 << 0)
  129. /* SATA PHY specific registers start and end address */
  130. #define RXCDRCALFOSC0 0x0065
  131. #define CALDUTY 0x006e
  132. #define RXDPIF 0x8065
  133. #define PPMDRIFTMAX_HI 0x80A4
  134. #define nlm_read_sata_reg(b, r) nlm_read_reg(b, r)
  135. #define nlm_write_sata_reg(b, r, v) nlm_write_reg(b, r, v)
  136. #define nlm_get_sata_pcibase(node) \
  137. nlm_pcicfg_base(XLP9XX_IO_SATA_OFFSET(node))
  138. #define nlm_get_sata_regbase(node) \
  139. (nlm_get_sata_pcibase(node) + 0x100)
  140. /* SATA PHY config for register block 1 0x0065 .. 0x006e */
  141. static const u8 sata_phy_config1[] = {
  142. 0xC9, 0xC9, 0x07, 0x07, 0x18, 0x18, 0x01, 0x01, 0x22, 0x00
  143. };
  144. /* SATA PHY config for register block 2 0x0x8065 .. 0x0x80A4 */
  145. static const u8 sata_phy_config2[] = {
  146. 0xAA, 0x00, 0x4C, 0xC9, 0xC9, 0x07, 0x07, 0x18,
  147. 0x18, 0x05, 0x0C, 0x10, 0x00, 0x10, 0x00, 0xFF,
  148. 0xCF, 0xF7, 0xE1, 0xF5, 0xFD, 0xFD, 0xFF, 0xFF,
  149. 0xFF, 0xFF, 0xE3, 0xE7, 0xDB, 0xF5, 0xFD, 0xFD,
  150. 0xF5, 0xF5, 0xFF, 0xFF, 0xE3, 0xE7, 0xDB, 0xF5,
  151. 0xFD, 0xFD, 0xF5, 0xF5, 0xFF, 0xFF, 0xFF, 0xF5,
  152. 0x3F, 0x00, 0x32, 0x00, 0x03, 0x01, 0x05, 0x05,
  153. 0x04, 0x00, 0x00, 0x08, 0x04, 0x00, 0x00, 0x04,
  154. };
  155. const int sata_phy_debug = 0; /* set to verify PHY writes */
  156. static void sata_clear_glue_reg(u64 regbase, u32 off, u32 bit)
  157. {
  158. u32 reg_val;
  159. reg_val = nlm_read_sata_reg(regbase, off);
  160. nlm_write_sata_reg(regbase, off, (reg_val & ~bit));
  161. }
  162. static void sata_set_glue_reg(u64 regbase, u32 off, u32 bit)
  163. {
  164. u32 reg_val;
  165. reg_val = nlm_read_sata_reg(regbase, off);
  166. nlm_write_sata_reg(regbase, off, (reg_val | bit));
  167. }
  168. static void write_phy_reg(u64 regbase, u32 addr, u32 physel, u8 data)
  169. {
  170. nlm_write_sata_reg(regbase, PHY_MEM_ACCESS,
  171. (1u << 31) | (physel << 24) | (data << 16) | addr);
  172. udelay(850);
  173. }
  174. static u8 read_phy_reg(u64 regbase, u32 addr, u32 physel)
  175. {
  176. u32 val;
  177. nlm_write_sata_reg(regbase, PHY_MEM_ACCESS,
  178. (0 << 31) | (physel << 24) | (0 << 16) | addr);
  179. udelay(850);
  180. val = nlm_read_sata_reg(regbase, PHY_MEM_ACCESS);
  181. return (val >> 16) & 0xff;
  182. }
  183. static void config_sata_phy(u64 regbase)
  184. {
  185. u32 port, i, reg;
  186. u8 val;
  187. for (port = 0; port < 2; port++) {
  188. for (i = 0, reg = RXCDRCALFOSC0; reg <= CALDUTY; reg++, i++)
  189. write_phy_reg(regbase, reg, port, sata_phy_config1[i]);
  190. for (i = 0, reg = RXDPIF; reg <= PPMDRIFTMAX_HI; reg++, i++)
  191. write_phy_reg(regbase, reg, port, sata_phy_config2[i]);
  192. /* Fix for PHY link up failures at lower temperatures */
  193. write_phy_reg(regbase, 0x800F, port, 0x1f);
  194. val = read_phy_reg(regbase, 0x0029, port);
  195. write_phy_reg(regbase, 0x0029, port, val | (0x7 << 1));
  196. val = read_phy_reg(regbase, 0x0056, port);
  197. write_phy_reg(regbase, 0x0056, port, val & ~(1 << 3));
  198. val = read_phy_reg(regbase, 0x0018, port);
  199. write_phy_reg(regbase, 0x0018, port, val & ~(0x7 << 0));
  200. }
  201. }
  202. static void check_phy_register(u64 regbase, u32 addr, u32 physel, u8 xdata)
  203. {
  204. u8 data;
  205. data = read_phy_reg(regbase, addr, physel);
  206. pr_info("PHY read addr = 0x%x physel = %d data = 0x%x %s\n",
  207. addr, physel, data, data == xdata ? "TRUE" : "FALSE");
  208. }
  209. static void verify_sata_phy_config(u64 regbase)
  210. {
  211. u32 port, i, reg;
  212. for (port = 0; port < 2; port++) {
  213. for (i = 0, reg = RXCDRCALFOSC0; reg <= CALDUTY; reg++, i++)
  214. check_phy_register(regbase, reg, port,
  215. sata_phy_config1[i]);
  216. for (i = 0, reg = RXDPIF; reg <= PPMDRIFTMAX_HI; reg++, i++)
  217. check_phy_register(regbase, reg, port,
  218. sata_phy_config2[i]);
  219. }
  220. }
  221. static void nlm_sata_firmware_init(int node)
  222. {
  223. u32 reg_val;
  224. u64 regbase;
  225. int n;
  226. pr_info("Initializing XLP9XX On-chip AHCI...\n");
  227. regbase = nlm_get_sata_regbase(node);
  228. /* Reset port0 */
  229. sata_clear_glue_reg(regbase, SATA_CTL, P0_IRST_POR);
  230. sata_clear_glue_reg(regbase, SATA_CTL, P0_IRST_HARD_TXRX);
  231. sata_clear_glue_reg(regbase, SATA_CTL, P0_IRST_HARD_SYNTH);
  232. sata_clear_glue_reg(regbase, SATA_CTL, P0_IPDTXL);
  233. sata_clear_glue_reg(regbase, SATA_CTL, P0_IPDRXL);
  234. sata_clear_glue_reg(regbase, SATA_CTL, P0_IPDIPDMSYNTH);
  235. /* port1 */
  236. sata_clear_glue_reg(regbase, SATA_CTL, P1_IRST_POR);
  237. sata_clear_glue_reg(regbase, SATA_CTL, P1_IRST_HARD_TXRX);
  238. sata_clear_glue_reg(regbase, SATA_CTL, P1_IRST_HARD_SYNTH);
  239. sata_clear_glue_reg(regbase, SATA_CTL, P1_IPDTXL);
  240. sata_clear_glue_reg(regbase, SATA_CTL, P1_IPDRXL);
  241. sata_clear_glue_reg(regbase, SATA_CTL, P1_IPDIPDMSYNTH);
  242. udelay(300);
  243. /* Set PHY */
  244. sata_set_glue_reg(regbase, SATA_CTL, P0_IPDTXL);
  245. sata_set_glue_reg(regbase, SATA_CTL, P0_IPDRXL);
  246. sata_set_glue_reg(regbase, SATA_CTL, P0_IPDIPDMSYNTH);
  247. sata_set_glue_reg(regbase, SATA_CTL, P1_IPDTXL);
  248. sata_set_glue_reg(regbase, SATA_CTL, P1_IPDRXL);
  249. sata_set_glue_reg(regbase, SATA_CTL, P1_IPDIPDMSYNTH);
  250. udelay(1000);
  251. sata_set_glue_reg(regbase, SATA_CTL, P0_IRST_POR);
  252. udelay(1000);
  253. sata_set_glue_reg(regbase, SATA_CTL, P1_IRST_POR);
  254. udelay(1000);
  255. /* setup PHY */
  256. config_sata_phy(regbase);
  257. if (sata_phy_debug)
  258. verify_sata_phy_config(regbase);
  259. udelay(1000);
  260. sata_set_glue_reg(regbase, SATA_CTL, P0_IRST_HARD_TXRX);
  261. sata_set_glue_reg(regbase, SATA_CTL, P0_IRST_HARD_SYNTH);
  262. sata_set_glue_reg(regbase, SATA_CTL, P1_IRST_HARD_TXRX);
  263. sata_set_glue_reg(regbase, SATA_CTL, P1_IRST_HARD_SYNTH);
  264. udelay(300);
  265. /* Override reset in serial PHY mode */
  266. sata_set_glue_reg(regbase, CR_REG_TIMER, CR_TIME_SCALE);
  267. /* Set reset SATA */
  268. sata_set_glue_reg(regbase, SATA_CTL, SATA_RST_N);
  269. sata_set_glue_reg(regbase, SATA_CTL, M_CSYSREQ);
  270. sata_set_glue_reg(regbase, SATA_CTL, S_CSYSREQ);
  271. pr_debug("Waiting for PHYs to come up.\n");
  272. n = 10000;
  273. do {
  274. reg_val = nlm_read_sata_reg(regbase, SATA_STATUS);
  275. if ((reg_val & P1_PHY_READY) && (reg_val & P0_PHY_READY))
  276. break;
  277. udelay(10);
  278. } while (--n > 0);
  279. if (reg_val & P0_PHY_READY)
  280. pr_info("PHY0 is up.\n");
  281. else
  282. pr_info("PHY0 is down.\n");
  283. if (reg_val & P1_PHY_READY)
  284. pr_info("PHY1 is up.\n");
  285. else
  286. pr_info("PHY1 is down.\n");
  287. pr_info("XLP AHCI Init Done.\n");
  288. }
  289. static int __init nlm_ahci_init(void)
  290. {
  291. int node;
  292. if (!cpu_is_xlp9xx())
  293. return 0;
  294. for (node = 0; node < NLM_NR_NODES; node++)
  295. if (nlm_node_present(node))
  296. nlm_sata_firmware_init(node);
  297. return 0;
  298. }
  299. static void nlm_sata_intr_ack(struct irq_data *data)
  300. {
  301. u64 regbase;
  302. u32 val;
  303. int node;
  304. node = data->irq / NLM_IRQS_PER_NODE;
  305. regbase = nlm_get_sata_regbase(node);
  306. val = nlm_read_sata_reg(regbase, SATA_INT);
  307. sata_set_glue_reg(regbase, SATA_INT, val);
  308. }
  309. static void nlm_sata_fixup_bar(struct pci_dev *dev)
  310. {
  311. dev->resource[5] = dev->resource[0];
  312. memset(&dev->resource[0], 0, sizeof(dev->resource[0]));
  313. }
  314. static void nlm_sata_fixup_final(struct pci_dev *dev)
  315. {
  316. u32 val;
  317. u64 regbase;
  318. int node;
  319. /* Find end bridge function to find node */
  320. node = xlp_socdev_to_node(dev);
  321. regbase = nlm_get_sata_regbase(node);
  322. /* clear pending interrupts and then enable them */
  323. val = nlm_read_sata_reg(regbase, SATA_INT);
  324. sata_set_glue_reg(regbase, SATA_INT, val);
  325. /* Enable only the core interrupt */
  326. sata_set_glue_reg(regbase, SATA_INT_MASK, 0x1);
  327. dev->irq = nlm_irq_to_xirq(node, PIC_SATA_IRQ);
  328. nlm_set_pic_extra_ack(node, PIC_SATA_IRQ, nlm_sata_intr_ack);
  329. }
  330. arch_initcall(nlm_ahci_init);
  331. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_XLP9XX_SATA,
  332. nlm_sata_fixup_bar);
  333. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_XLP9XX_SATA,
  334. nlm_sata_fixup_final);