ahci-init.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <asm/cpu.h>
  42. #include <asm/mipsregs.h>
  43. #include <asm/netlogic/haldefs.h>
  44. #include <asm/netlogic/xlp-hal/xlp.h>
  45. #include <asm/netlogic/common.h>
  46. #include <asm/netlogic/xlp-hal/iomap.h>
  47. #include <asm/netlogic/mips-extns.h>
  48. #define SATA_CTL 0x0
  49. #define SATA_STATUS 0x1 /* Status Reg */
  50. #define SATA_INT 0x2 /* Interrupt Reg */
  51. #define SATA_INT_MASK 0x3 /* Interrupt Mask Reg */
  52. #define SATA_CR_REG_TIMER 0x4 /* PHY Conrol Timer Reg */
  53. #define SATA_CORE_ID 0x5 /* Core ID Reg */
  54. #define SATA_AXI_SLAVE_OPT1 0x6 /* AXI Slave Options Reg */
  55. #define SATA_PHY_LOS_LEV 0x7 /* PHY LOS Level Reg */
  56. #define SATA_PHY_MULTI 0x8 /* PHY Multiplier Reg */
  57. #define SATA_PHY_CLK_SEL 0x9 /* Clock Select Reg */
  58. #define SATA_PHY_AMP1_GEN1 0xa /* PHY Transmit Amplitude Reg 1 */
  59. #define SATA_PHY_AMP1_GEN2 0xb /* PHY Transmit Amplitude Reg 2 */
  60. #define SATA_PHY_AMP1_GEN3 0xc /* PHY Transmit Amplitude Reg 3 */
  61. #define SATA_PHY_PRE1 0xd /* PHY Transmit Preemphasis Reg 1 */
  62. #define SATA_PHY_PRE2 0xe /* PHY Transmit Preemphasis Reg 2 */
  63. #define SATA_PHY_PRE3 0xf /* PHY Transmit Preemphasis Reg 3 */
  64. #define SATA_SPDMODE 0x10 /* Speed Mode Reg */
  65. #define SATA_REFCLK 0x11 /* Reference Clock Control Reg */
  66. #define SATA_BYTE_SWAP_DIS 0x12 /* byte swap disable */
  67. /*SATA_CTL Bits */
  68. #define SATA_RST_N BIT(0)
  69. #define PHY0_RESET_N BIT(16)
  70. #define PHY1_RESET_N BIT(17)
  71. #define PHY2_RESET_N BIT(18)
  72. #define PHY3_RESET_N BIT(19)
  73. #define M_CSYSREQ BIT(2)
  74. #define S_CSYSREQ BIT(3)
  75. /*SATA_STATUS Bits */
  76. #define P0_PHY_READY BIT(4)
  77. #define P1_PHY_READY BIT(5)
  78. #define P2_PHY_READY BIT(6)
  79. #define P3_PHY_READY BIT(7)
  80. #define nlm_read_sata_reg(b, r) nlm_read_reg(b, r)
  81. #define nlm_write_sata_reg(b, r, v) nlm_write_reg(b, r, v)
  82. #define nlm_get_sata_pcibase(node) \
  83. nlm_pcicfg_base(XLP_IO_SATA_OFFSET(node))
  84. /* SATA device specific configuration registers are starts at 0x900 offset */
  85. #define nlm_get_sata_regbase(node) \
  86. (nlm_get_sata_pcibase(node) + 0x900)
  87. static void sata_clear_glue_reg(uint64_t regbase, uint32_t off, uint32_t bit)
  88. {
  89. uint32_t reg_val;
  90. reg_val = nlm_read_sata_reg(regbase, off);
  91. nlm_write_sata_reg(regbase, off, (reg_val & ~bit));
  92. }
  93. static void sata_set_glue_reg(uint64_t regbase, uint32_t off, uint32_t bit)
  94. {
  95. uint32_t reg_val;
  96. reg_val = nlm_read_sata_reg(regbase, off);
  97. nlm_write_sata_reg(regbase, off, (reg_val | bit));
  98. }
  99. static void nlm_sata_firmware_init(int node)
  100. {
  101. uint32_t reg_val;
  102. uint64_t regbase;
  103. int i;
  104. pr_info("XLP AHCI Initialization started.\n");
  105. regbase = nlm_get_sata_regbase(node);
  106. /* Reset SATA */
  107. sata_clear_glue_reg(regbase, SATA_CTL, SATA_RST_N);
  108. /* Reset PHY */
  109. sata_clear_glue_reg(regbase, SATA_CTL,
  110. (PHY3_RESET_N | PHY2_RESET_N
  111. | PHY1_RESET_N | PHY0_RESET_N));
  112. /* Set SATA */
  113. sata_set_glue_reg(regbase, SATA_CTL, SATA_RST_N);
  114. /* Set PHY */
  115. sata_set_glue_reg(regbase, SATA_CTL,
  116. (PHY3_RESET_N | PHY2_RESET_N
  117. | PHY1_RESET_N | PHY0_RESET_N));
  118. pr_debug("Waiting for PHYs to come up.\n");
  119. i = 0;
  120. do {
  121. reg_val = nlm_read_sata_reg(regbase, SATA_STATUS);
  122. i++;
  123. } while (((reg_val & 0xF0) != 0xF0) && (i < 10000));
  124. for (i = 0; i < 4; i++) {
  125. if (reg_val & (P0_PHY_READY << i))
  126. pr_info("PHY%d is up.\n", i);
  127. else
  128. pr_info("PHY%d is down.\n", i);
  129. }
  130. pr_info("XLP AHCI init done.\n");
  131. }
  132. static int __init nlm_ahci_init(void)
  133. {
  134. int node = 0;
  135. int chip = read_c0_prid() & PRID_IMP_MASK;
  136. if (chip == PRID_IMP_NETLOGIC_XLP3XX)
  137. nlm_sata_firmware_init(node);
  138. return 0;
  139. }
  140. static void nlm_sata_intr_ack(struct irq_data *data)
  141. {
  142. uint32_t val = 0;
  143. uint64_t regbase;
  144. regbase = nlm_get_sata_regbase(nlm_nodeid());
  145. val = nlm_read_sata_reg(regbase, SATA_INT);
  146. sata_set_glue_reg(regbase, SATA_INT, val);
  147. }
  148. static void nlm_sata_fixup_bar(struct pci_dev *dev)
  149. {
  150. /*
  151. * The AHCI resource is in BAR 0, move it to
  152. * BAR 5, where it is expected
  153. */
  154. dev->resource[5] = dev->resource[0];
  155. memset(&dev->resource[0], 0, sizeof(dev->resource[0]));
  156. }
  157. static void nlm_sata_fixup_final(struct pci_dev *dev)
  158. {
  159. uint32_t val;
  160. uint64_t regbase;
  161. int node = 0; /* XLP3XX does not support multi-node */
  162. regbase = nlm_get_sata_regbase(node);
  163. /* clear pending interrupts and then enable them */
  164. val = nlm_read_sata_reg(regbase, SATA_INT);
  165. sata_set_glue_reg(regbase, SATA_INT, val);
  166. /* Mask the core interrupt. If all the interrupts
  167. * are enabled there are spurious interrupt flow
  168. * happening, to avoid only enable core interrupt
  169. * mask.
  170. */
  171. sata_set_glue_reg(regbase, SATA_INT_MASK, 0x1);
  172. dev->irq = PIC_SATA_IRQ;
  173. nlm_set_pic_extra_ack(node, PIC_SATA_IRQ, nlm_sata_intr_ack);
  174. }
  175. arch_initcall(nlm_ahci_init);
  176. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_NETLOGIC, PCI_DEVICE_ID_NLM_SATA,
  177. nlm_sata_fixup_bar);
  178. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_NETLOGIC, PCI_DEVICE_ID_NLM_SATA,
  179. nlm_sata_fixup_final);