pcie-designware.h 11 KB

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