pcie-designware.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Synopsys DesignWare PCIe host controller driver
  4. *
  5. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com
  7. *
  8. * Author: Jingoo Han <jg1.han@samsung.com>
  9. */
  10. #ifndef _PCIE_DESIGNWARE_H
  11. #define _PCIE_DESIGNWARE_H
  12. #include <linux/dma-mapping.h>
  13. #include <linux/irq.h>
  14. #include <linux/msi.h>
  15. #include <linux/pci.h>
  16. #include <linux/pci-epc.h>
  17. #include <linux/pci-epf.h>
  18. /* Parameters for the waiting for link up routine */
  19. #define LINK_WAIT_MAX_RETRIES 10
  20. #define LINK_WAIT_USLEEP_MIN 90000
  21. #define LINK_WAIT_USLEEP_MAX 100000
  22. /* Parameters for the waiting for iATU enabled routine */
  23. #define LINK_WAIT_MAX_IATU_RETRIES 5
  24. #define LINK_WAIT_IATU_MIN 9000
  25. #define LINK_WAIT_IATU_MAX 10000
  26. /* Synopsys-specific PCIe configuration registers */
  27. #define PCIE_PORT_LINK_CONTROL 0x710
  28. #define PORT_LINK_MODE_MASK (0x3f << 16)
  29. #define PORT_LINK_MODE_1_LANES (0x1 << 16)
  30. #define PORT_LINK_MODE_2_LANES (0x3 << 16)
  31. #define PORT_LINK_MODE_4_LANES (0x7 << 16)
  32. #define PORT_LINK_MODE_8_LANES (0xf << 16)
  33. #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
  34. #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
  35. #define PORT_LOGIC_LINK_WIDTH_MASK (0x1f << 8)
  36. #define PORT_LOGIC_LINK_WIDTH_1_LANES (0x1 << 8)
  37. #define PORT_LOGIC_LINK_WIDTH_2_LANES (0x2 << 8)
  38. #define PORT_LOGIC_LINK_WIDTH_4_LANES (0x4 << 8)
  39. #define PORT_LOGIC_LINK_WIDTH_8_LANES (0x8 << 8)
  40. #define PCIE_MSI_ADDR_LO 0x820
  41. #define PCIE_MSI_ADDR_HI 0x824
  42. #define PCIE_MSI_INTR0_ENABLE 0x828
  43. #define PCIE_MSI_INTR0_MASK 0x82C
  44. #define PCIE_MSI_INTR0_STATUS 0x830
  45. #define PCIE_ATU_VIEWPORT 0x900
  46. #define PCIE_ATU_REGION_INBOUND (0x1 << 31)
  47. #define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
  48. #define PCIE_ATU_REGION_INDEX2 (0x2 << 0)
  49. #define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
  50. #define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
  51. #define PCIE_ATU_CR1 0x904
  52. #define PCIE_ATU_TYPE_MEM (0x0 << 0)
  53. #define PCIE_ATU_TYPE_IO (0x2 << 0)
  54. #define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
  55. #define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
  56. #define PCIE_ATU_CR2 0x908
  57. #define PCIE_ATU_ENABLE (0x1 << 31)
  58. #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
  59. #define PCIE_ATU_LOWER_BASE 0x90C
  60. #define PCIE_ATU_UPPER_BASE 0x910
  61. #define PCIE_ATU_LIMIT 0x914
  62. #define PCIE_ATU_LOWER_TARGET 0x918
  63. #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
  64. #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
  65. #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
  66. #define PCIE_ATU_UPPER_TARGET 0x91C
  67. #define PCIE_MISC_CONTROL_1_OFF 0x8BC
  68. #define PCIE_DBI_RO_WR_EN (0x1 << 0)
  69. /*
  70. * iATU Unroll-specific register definitions
  71. * From 4.80 core version the address translation will be made by unroll
  72. */
  73. #define PCIE_ATU_UNR_REGION_CTRL1 0x00
  74. #define PCIE_ATU_UNR_REGION_CTRL2 0x04
  75. #define PCIE_ATU_UNR_LOWER_BASE 0x08
  76. #define PCIE_ATU_UNR_UPPER_BASE 0x0C
  77. #define PCIE_ATU_UNR_LIMIT 0x10
  78. #define PCIE_ATU_UNR_LOWER_TARGET 0x14
  79. #define PCIE_ATU_UNR_UPPER_TARGET 0x18
  80. /* Register address builder */
  81. #define PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(region) \
  82. ((0x3 << 20) | ((region) << 9))
  83. #define PCIE_GET_ATU_INB_UNR_REG_OFFSET(region) \
  84. ((0x3 << 20) | ((region) << 9) | (0x1 << 8))
  85. #define MSI_MESSAGE_CONTROL 0x52
  86. #define MSI_CAP_MMC_SHIFT 1
  87. #define MSI_CAP_MMC_MASK (7 << MSI_CAP_MMC_SHIFT)
  88. #define MSI_CAP_MME_SHIFT 4
  89. #define MSI_CAP_MSI_EN_MASK 0x1
  90. #define MSI_CAP_MME_MASK (7 << MSI_CAP_MME_SHIFT)
  91. #define MSI_MESSAGE_ADDR_L32 0x54
  92. #define MSI_MESSAGE_ADDR_U32 0x58
  93. #define MSI_MESSAGE_DATA_32 0x58
  94. #define MSI_MESSAGE_DATA_64 0x5C
  95. #define MAX_MSI_IRQS 256
  96. #define MAX_MSI_IRQS_PER_CTRL 32
  97. #define MAX_MSI_CTRLS (MAX_MSI_IRQS / MAX_MSI_IRQS_PER_CTRL)
  98. #define MSI_REG_CTRL_BLOCK_SIZE 12
  99. #define MSI_DEF_NUM_VECTORS 32
  100. /* Maximum number of inbound/outbound iATUs */
  101. #define MAX_IATU_IN 256
  102. #define MAX_IATU_OUT 256
  103. struct pcie_port;
  104. struct dw_pcie;
  105. struct dw_pcie_ep;
  106. enum dw_pcie_region_type {
  107. DW_PCIE_REGION_UNKNOWN,
  108. DW_PCIE_REGION_INBOUND,
  109. DW_PCIE_REGION_OUTBOUND,
  110. };
  111. enum dw_pcie_device_mode {
  112. DW_PCIE_UNKNOWN_TYPE,
  113. DW_PCIE_EP_TYPE,
  114. DW_PCIE_LEG_EP_TYPE,
  115. DW_PCIE_RC_TYPE,
  116. };
  117. struct dw_pcie_host_ops {
  118. int (*rd_own_conf)(struct pcie_port *pp, int where, int size, u32 *val);
  119. int (*wr_own_conf)(struct pcie_port *pp, int where, int size, u32 val);
  120. int (*rd_other_conf)(struct pcie_port *pp, struct pci_bus *bus,
  121. unsigned int devfn, int where, int size, u32 *val);
  122. int (*wr_other_conf)(struct pcie_port *pp, struct pci_bus *bus,
  123. unsigned int devfn, int where, int size, u32 val);
  124. int (*host_init)(struct pcie_port *pp);
  125. void (*msi_set_irq)(struct pcie_port *pp, int irq);
  126. void (*msi_clear_irq)(struct pcie_port *pp, int irq);
  127. phys_addr_t (*get_msi_addr)(struct pcie_port *pp);
  128. u32 (*get_msi_data)(struct pcie_port *pp, int pos);
  129. void (*scan_bus)(struct pcie_port *pp);
  130. void (*set_num_vectors)(struct pcie_port *pp);
  131. int (*msi_host_init)(struct pcie_port *pp);
  132. void (*msi_irq_ack)(int irq, struct pcie_port *pp);
  133. };
  134. struct pcie_port {
  135. u8 root_bus_nr;
  136. u64 cfg0_base;
  137. void __iomem *va_cfg0_base;
  138. u32 cfg0_size;
  139. u64 cfg1_base;
  140. void __iomem *va_cfg1_base;
  141. u32 cfg1_size;
  142. resource_size_t io_base;
  143. phys_addr_t io_bus_addr;
  144. u32 io_size;
  145. u64 mem_base;
  146. phys_addr_t mem_bus_addr;
  147. u32 mem_size;
  148. struct resource *cfg;
  149. struct resource *io;
  150. struct resource *mem;
  151. struct resource *busn;
  152. int irq;
  153. const struct dw_pcie_host_ops *ops;
  154. int msi_irq;
  155. struct irq_domain *irq_domain;
  156. struct irq_domain *msi_domain;
  157. dma_addr_t msi_data;
  158. u32 num_vectors;
  159. u32 irq_status[MAX_MSI_CTRLS];
  160. raw_spinlock_t lock;
  161. DECLARE_BITMAP(msi_irq_in_use, MAX_MSI_IRQS);
  162. };
  163. enum dw_pcie_as_type {
  164. DW_PCIE_AS_UNKNOWN,
  165. DW_PCIE_AS_MEM,
  166. DW_PCIE_AS_IO,
  167. };
  168. struct dw_pcie_ep_ops {
  169. void (*ep_init)(struct dw_pcie_ep *ep);
  170. int (*raise_irq)(struct dw_pcie_ep *ep, u8 func_no,
  171. enum pci_epc_irq_type type, u8 interrupt_num);
  172. };
  173. struct dw_pcie_ep {
  174. struct pci_epc *epc;
  175. struct dw_pcie_ep_ops *ops;
  176. phys_addr_t phys_base;
  177. size_t addr_size;
  178. size_t page_size;
  179. u8 bar_to_atu[6];
  180. phys_addr_t *outbound_addr;
  181. unsigned long *ib_window_map;
  182. unsigned long *ob_window_map;
  183. u32 num_ib_windows;
  184. u32 num_ob_windows;
  185. void __iomem *msi_mem;
  186. phys_addr_t msi_mem_phys;
  187. };
  188. struct dw_pcie_ops {
  189. u64 (*cpu_addr_fixup)(struct dw_pcie *pcie, u64 cpu_addr);
  190. u32 (*read_dbi)(struct dw_pcie *pcie, void __iomem *base, u32 reg,
  191. size_t size);
  192. void (*write_dbi)(struct dw_pcie *pcie, void __iomem *base, u32 reg,
  193. size_t size, u32 val);
  194. int (*link_up)(struct dw_pcie *pcie);
  195. int (*start_link)(struct dw_pcie *pcie);
  196. void (*stop_link)(struct dw_pcie *pcie);
  197. };
  198. struct dw_pcie {
  199. struct device *dev;
  200. void __iomem *dbi_base;
  201. void __iomem *dbi_base2;
  202. u32 num_viewport;
  203. u8 iatu_unroll_enabled;
  204. struct pcie_port pp;
  205. struct dw_pcie_ep ep;
  206. const struct dw_pcie_ops *ops;
  207. };
  208. #define to_dw_pcie_from_pp(port) container_of((port), struct dw_pcie, pp)
  209. #define to_dw_pcie_from_ep(endpoint) \
  210. container_of((endpoint), struct dw_pcie, ep)
  211. int dw_pcie_read(void __iomem *addr, int size, u32 *val);
  212. int dw_pcie_write(void __iomem *addr, int size, u32 val);
  213. u32 __dw_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base, u32 reg,
  214. size_t size);
  215. void __dw_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base, u32 reg,
  216. size_t size, u32 val);
  217. int dw_pcie_link_up(struct dw_pcie *pci);
  218. int dw_pcie_wait_for_link(struct dw_pcie *pci);
  219. void dw_pcie_prog_outbound_atu(struct dw_pcie *pci, int index,
  220. int type, u64 cpu_addr, u64 pci_addr,
  221. u32 size);
  222. int dw_pcie_prog_inbound_atu(struct dw_pcie *pci, int index, int bar,
  223. u64 cpu_addr, enum dw_pcie_as_type as_type);
  224. void dw_pcie_disable_atu(struct dw_pcie *pci, int index,
  225. enum dw_pcie_region_type type);
  226. void dw_pcie_setup(struct dw_pcie *pci);
  227. static inline void dw_pcie_writel_dbi(struct dw_pcie *pci, u32 reg, u32 val)
  228. {
  229. __dw_pcie_write_dbi(pci, pci->dbi_base, reg, 0x4, val);
  230. }
  231. static inline u32 dw_pcie_readl_dbi(struct dw_pcie *pci, u32 reg)
  232. {
  233. return __dw_pcie_read_dbi(pci, pci->dbi_base, reg, 0x4);
  234. }
  235. static inline void dw_pcie_writew_dbi(struct dw_pcie *pci, u32 reg, u16 val)
  236. {
  237. __dw_pcie_write_dbi(pci, pci->dbi_base, reg, 0x2, val);
  238. }
  239. static inline u16 dw_pcie_readw_dbi(struct dw_pcie *pci, u32 reg)
  240. {
  241. return __dw_pcie_read_dbi(pci, pci->dbi_base, reg, 0x2);
  242. }
  243. static inline void dw_pcie_writeb_dbi(struct dw_pcie *pci, u32 reg, u8 val)
  244. {
  245. __dw_pcie_write_dbi(pci, pci->dbi_base, reg, 0x1, val);
  246. }
  247. static inline u8 dw_pcie_readb_dbi(struct dw_pcie *pci, u32 reg)
  248. {
  249. return __dw_pcie_read_dbi(pci, pci->dbi_base, reg, 0x1);
  250. }
  251. static inline void dw_pcie_writel_dbi2(struct dw_pcie *pci, u32 reg, u32 val)
  252. {
  253. __dw_pcie_write_dbi(pci, pci->dbi_base2, reg, 0x4, val);
  254. }
  255. static inline u32 dw_pcie_readl_dbi2(struct dw_pcie *pci, u32 reg)
  256. {
  257. return __dw_pcie_read_dbi(pci, pci->dbi_base2, reg, 0x4);
  258. }
  259. static inline void dw_pcie_dbi_ro_wr_en(struct dw_pcie *pci)
  260. {
  261. u32 reg;
  262. u32 val;
  263. reg = PCIE_MISC_CONTROL_1_OFF;
  264. val = dw_pcie_readl_dbi(pci, reg);
  265. val |= PCIE_DBI_RO_WR_EN;
  266. dw_pcie_writel_dbi(pci, reg, val);
  267. }
  268. static inline void dw_pcie_dbi_ro_wr_dis(struct dw_pcie *pci)
  269. {
  270. u32 reg;
  271. u32 val;
  272. reg = PCIE_MISC_CONTROL_1_OFF;
  273. val = dw_pcie_readl_dbi(pci, reg);
  274. val &= ~PCIE_DBI_RO_WR_EN;
  275. dw_pcie_writel_dbi(pci, reg, val);
  276. }
  277. #ifdef CONFIG_PCIE_DW_HOST
  278. irqreturn_t dw_handle_msi_irq(struct pcie_port *pp);
  279. void dw_pcie_msi_init(struct pcie_port *pp);
  280. void dw_pcie_free_msi(struct pcie_port *pp);
  281. void dw_pcie_setup_rc(struct pcie_port *pp);
  282. int dw_pcie_host_init(struct pcie_port *pp);
  283. int dw_pcie_allocate_domains(struct pcie_port *pp);
  284. #else
  285. static inline irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
  286. {
  287. return IRQ_NONE;
  288. }
  289. static inline void dw_pcie_msi_init(struct pcie_port *pp)
  290. {
  291. }
  292. static inline void dw_pcie_free_msi(struct pcie_port *pp)
  293. {
  294. }
  295. static inline void dw_pcie_setup_rc(struct pcie_port *pp)
  296. {
  297. }
  298. static inline int dw_pcie_host_init(struct pcie_port *pp)
  299. {
  300. return 0;
  301. }
  302. static inline int dw_pcie_allocate_domains(struct pcie_port *pp)
  303. {
  304. return 0;
  305. }
  306. #endif
  307. #ifdef CONFIG_PCIE_DW_EP
  308. void dw_pcie_ep_linkup(struct dw_pcie_ep *ep);
  309. int dw_pcie_ep_init(struct dw_pcie_ep *ep);
  310. void dw_pcie_ep_exit(struct dw_pcie_ep *ep);
  311. int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no,
  312. u8 interrupt_num);
  313. void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar);
  314. #else
  315. static inline void dw_pcie_ep_linkup(struct dw_pcie_ep *ep)
  316. {
  317. }
  318. static inline int dw_pcie_ep_init(struct dw_pcie_ep *ep)
  319. {
  320. return 0;
  321. }
  322. static inline void dw_pcie_ep_exit(struct dw_pcie_ep *ep)
  323. {
  324. }
  325. static inline int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no,
  326. u8 interrupt_num)
  327. {
  328. return 0;
  329. }
  330. static inline void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar)
  331. {
  332. }
  333. #endif
  334. #endif /* _PCIE_DESIGNWARE_H */