pcie-designware.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Synopsys Designware PCIe host controller driver
  3. *
  4. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com
  6. *
  7. * Author: Jingoo Han <jg1.han@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #ifndef _PCIE_DESIGNWARE_H
  14. #define _PCIE_DESIGNWARE_H
  15. #include <linux/irq.h>
  16. #include <linux/msi.h>
  17. #include <linux/pci.h>
  18. #include <linux/pci-epc.h>
  19. #include <linux/pci-epf.h>
  20. /* Parameters for the waiting for link up routine */
  21. #define LINK_WAIT_MAX_RETRIES 10
  22. #define LINK_WAIT_USLEEP_MIN 90000
  23. #define LINK_WAIT_USLEEP_MAX 100000
  24. /* Parameters for the waiting for iATU enabled routine */
  25. #define LINK_WAIT_MAX_IATU_RETRIES 5
  26. #define LINK_WAIT_IATU_MIN 9000
  27. #define LINK_WAIT_IATU_MAX 10000
  28. /* Synopsys-specific PCIe configuration registers */
  29. #define PCIE_PORT_LINK_CONTROL 0x710
  30. #define PORT_LINK_MODE_MASK (0x3f << 16)
  31. #define PORT_LINK_MODE_1_LANES (0x1 << 16)
  32. #define PORT_LINK_MODE_2_LANES (0x3 << 16)
  33. #define PORT_LINK_MODE_4_LANES (0x7 << 16)
  34. #define PORT_LINK_MODE_8_LANES (0xf << 16)
  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. /*
  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 MSI_MESSAGE_CONTROL 0x52
  84. #define MSI_CAP_MMC_SHIFT 1
  85. #define MSI_CAP_MME_SHIFT 4
  86. #define MSI_CAP_MME_MASK (7 << MSI_CAP_MME_SHIFT)
  87. #define MSI_MESSAGE_ADDR_L32 0x54
  88. #define MSI_MESSAGE_ADDR_U32 0x58
  89. /*
  90. * Maximum number of MSI IRQs can be 256 per controller. But keep
  91. * it 32 as of now. Probably we will never need more than 32. If needed,
  92. * then increment it in multiple of 32.
  93. */
  94. #define MAX_MSI_IRQS 32
  95. #define MAX_MSI_CTRLS (MAX_MSI_IRQS / 32)
  96. struct pcie_port;
  97. struct dw_pcie;
  98. struct dw_pcie_ep;
  99. enum dw_pcie_region_type {
  100. DW_PCIE_REGION_UNKNOWN,
  101. DW_PCIE_REGION_INBOUND,
  102. DW_PCIE_REGION_OUTBOUND,
  103. };
  104. enum dw_pcie_device_mode {
  105. DW_PCIE_UNKNOWN_TYPE,
  106. DW_PCIE_EP_TYPE,
  107. DW_PCIE_LEG_EP_TYPE,
  108. DW_PCIE_RC_TYPE,
  109. };
  110. struct dw_pcie_host_ops {
  111. int (*rd_own_conf)(struct pcie_port *pp, int where, int size, u32 *val);
  112. int (*wr_own_conf)(struct pcie_port *pp, int where, int size, u32 val);
  113. int (*rd_other_conf)(struct pcie_port *pp, struct pci_bus *bus,
  114. unsigned int devfn, int where, int size, u32 *val);
  115. int (*wr_other_conf)(struct pcie_port *pp, struct pci_bus *bus,
  116. unsigned int devfn, int where, int size, u32 val);
  117. void (*host_init)(struct pcie_port *pp);
  118. void (*msi_set_irq)(struct pcie_port *pp, int irq);
  119. void (*msi_clear_irq)(struct pcie_port *pp, int irq);
  120. phys_addr_t (*get_msi_addr)(struct pcie_port *pp);
  121. u32 (*get_msi_data)(struct pcie_port *pp, int pos);
  122. void (*scan_bus)(struct pcie_port *pp);
  123. int (*msi_host_init)(struct pcie_port *pp, struct msi_controller *chip);
  124. };
  125. struct pcie_port {
  126. u8 root_bus_nr;
  127. u64 cfg0_base;
  128. void __iomem *va_cfg0_base;
  129. u32 cfg0_size;
  130. u64 cfg1_base;
  131. void __iomem *va_cfg1_base;
  132. u32 cfg1_size;
  133. resource_size_t io_base;
  134. phys_addr_t io_bus_addr;
  135. u32 io_size;
  136. u64 mem_base;
  137. phys_addr_t mem_bus_addr;
  138. u32 mem_size;
  139. struct resource *cfg;
  140. struct resource *io;
  141. struct resource *mem;
  142. struct resource *busn;
  143. int irq;
  144. struct dw_pcie_host_ops *ops;
  145. int msi_irq;
  146. struct irq_domain *irq_domain;
  147. unsigned long msi_data;
  148. DECLARE_BITMAP(msi_irq_in_use, MAX_MSI_IRQS);
  149. };
  150. enum dw_pcie_as_type {
  151. DW_PCIE_AS_UNKNOWN,
  152. DW_PCIE_AS_MEM,
  153. DW_PCIE_AS_IO,
  154. };
  155. struct dw_pcie_ep_ops {
  156. void (*ep_init)(struct dw_pcie_ep *ep);
  157. int (*raise_irq)(struct dw_pcie_ep *ep, enum pci_epc_irq_type type,
  158. u8 interrupt_num);
  159. };
  160. struct dw_pcie_ep {
  161. struct pci_epc *epc;
  162. struct dw_pcie_ep_ops *ops;
  163. phys_addr_t phys_base;
  164. size_t addr_size;
  165. size_t page_size;
  166. u8 bar_to_atu[6];
  167. phys_addr_t *outbound_addr;
  168. unsigned long ib_window_map;
  169. unsigned long ob_window_map;
  170. u32 num_ib_windows;
  171. u32 num_ob_windows;
  172. };
  173. struct dw_pcie_ops {
  174. u64 (*cpu_addr_fixup)(u64 cpu_addr);
  175. u32 (*read_dbi)(struct dw_pcie *pcie, void __iomem *base, u32 reg,
  176. size_t size);
  177. void (*write_dbi)(struct dw_pcie *pcie, void __iomem *base, u32 reg,
  178. size_t size, u32 val);
  179. u32 (*read_dbi2)(struct dw_pcie *pcie, void __iomem *base, u32 reg,
  180. size_t size);
  181. void (*write_dbi2)(struct dw_pcie *pcie, void __iomem *base, u32 reg,
  182. size_t size, u32 val);
  183. int (*link_up)(struct dw_pcie *pcie);
  184. int (*start_link)(struct dw_pcie *pcie);
  185. void (*stop_link)(struct dw_pcie *pcie);
  186. int (*inbound_atu)(struct dw_pcie *pci, u32 index,
  187. enum pci_barno bar, dma_addr_t cpu_addr);
  188. int (*outbound_atu)(struct dw_pcie *pci, u64 cpu_addr,
  189. u64 pci_addr, size_t size);
  190. void (*disable_atu)(struct dw_pcie *pci, phys_addr_t addr, int index,
  191. enum dw_pcie_region_type type);
  192. };
  193. struct dw_pcie {
  194. struct device *dev;
  195. void __iomem *dbi_base;
  196. void __iomem *dbi_base2;
  197. u32 num_viewport;
  198. u8 iatu_unroll_enabled;
  199. struct pcie_port pp;
  200. struct dw_pcie_ep ep;
  201. const struct dw_pcie_ops *ops;
  202. };
  203. #define to_dw_pcie_from_pp(port) container_of((port), struct dw_pcie, pp)
  204. #define to_dw_pcie_from_ep(endpoint) \
  205. container_of((endpoint), struct dw_pcie, ep)
  206. int dw_pcie_read(void __iomem *addr, int size, u32 *val);
  207. int dw_pcie_write(void __iomem *addr, int size, u32 val);
  208. u32 __dw_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base, u32 reg,
  209. size_t size);
  210. void __dw_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base, u32 reg,
  211. size_t size, u32 val);
  212. u32 __dw_pcie_read_dbi2(struct dw_pcie *pci, void __iomem *base, u32 reg,
  213. size_t size);
  214. void __dw_pcie_write_dbi2(struct dw_pcie *pci, void __iomem *base, u32 reg,
  215. size_t size, u32 val);
  216. int dw_pcie_link_up(struct dw_pcie *pci);
  217. int dw_pcie_wait_for_link(struct dw_pcie *pci);
  218. int dw_pcie_prog_outbound_atu(struct dw_pcie *pci, int index,
  219. int type, u64 cpu_addr, u64 pci_addr,
  220. u32 size);
  221. int dw_pcie_prog_inbound_atu(struct dw_pcie *pci, int index, int bar,
  222. u64 cpu_addr, enum dw_pcie_as_type as_type);
  223. void dw_pcie_disable_atu(struct dw_pcie *pci, int index,
  224. enum dw_pcie_region_type type);
  225. void dw_pcie_setup(struct dw_pcie *pci);
  226. static inline void dw_pcie_writel_dbi(struct dw_pcie *pci, u32 reg, u32 val)
  227. {
  228. __dw_pcie_write_dbi(pci, pci->dbi_base, reg, 0x4, val);
  229. }
  230. static inline u32 dw_pcie_readl_dbi(struct dw_pcie *pci, u32 reg)
  231. {
  232. return __dw_pcie_read_dbi(pci, pci->dbi_base, reg, 0x4);
  233. }
  234. static inline void dw_pcie_writew_dbi(struct dw_pcie *pci, u32 reg, u16 val)
  235. {
  236. __dw_pcie_write_dbi(pci, pci->dbi_base, reg, 0x2, val);
  237. }
  238. static inline u16 dw_pcie_readw_dbi(struct dw_pcie *pci, u32 reg)
  239. {
  240. return __dw_pcie_read_dbi(pci, pci->dbi_base, reg, 0x2);
  241. }
  242. static inline void dw_pcie_writeb_dbi(struct dw_pcie *pci, u32 reg, u8 val)
  243. {
  244. __dw_pcie_write_dbi(pci, pci->dbi_base, reg, 0x1, val);
  245. }
  246. static inline u8 dw_pcie_readb_dbi(struct dw_pcie *pci, u32 reg)
  247. {
  248. return __dw_pcie_read_dbi(pci, pci->dbi_base, reg, 0x1);
  249. }
  250. static inline void dw_pcie_writel_dbi2(struct dw_pcie *pci, u32 reg, u32 val)
  251. {
  252. __dw_pcie_write_dbi2(pci, pci->dbi_base2, reg, 0x4, val);
  253. }
  254. static inline u32 dw_pcie_readl_dbi2(struct dw_pcie *pci, u32 reg)
  255. {
  256. return __dw_pcie_read_dbi2(pci, pci->dbi_base2, reg, 0x4);
  257. }
  258. #ifdef CONFIG_PCIE_DW_HOST
  259. irqreturn_t dw_handle_msi_irq(struct pcie_port *pp);
  260. void dw_pcie_msi_init(struct pcie_port *pp);
  261. void dw_pcie_setup_rc(struct pcie_port *pp);
  262. int dw_pcie_host_init(struct pcie_port *pp);
  263. #else
  264. static inline irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
  265. {
  266. return IRQ_NONE;
  267. }
  268. static inline void dw_pcie_msi_init(struct pcie_port *pp)
  269. {
  270. }
  271. static inline void dw_pcie_setup_rc(struct pcie_port *pp)
  272. {
  273. }
  274. static inline int dw_pcie_host_init(struct pcie_port *pp)
  275. {
  276. return 0;
  277. }
  278. #endif
  279. #ifdef CONFIG_PCIE_DW_EP
  280. void dw_pcie_ep_linkup(struct dw_pcie_ep *ep);
  281. int dw_pcie_ep_init(struct dw_pcie_ep *ep);
  282. void dw_pcie_ep_exit(struct dw_pcie_ep *ep);
  283. #else
  284. static inline void dw_pcie_ep_linkup(struct dw_pcie_ep *ep)
  285. {
  286. }
  287. static inline int dw_pcie_ep_init(struct dw_pcie_ep *ep)
  288. {
  289. return 0;
  290. }
  291. static inline void dw_pcie_ep_exit(struct dw_pcie_ep *ep)
  292. {
  293. }
  294. #endif
  295. #endif /* _PCIE_DESIGNWARE_H */