pcie-designware.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * PCIe host controller driver for Samsung EXYNOS SoCs
  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. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/gpio.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/list.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/of_pci.h>
  24. #include <linux/pci.h>
  25. #include <linux/pci_regs.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/resource.h>
  28. #include <linux/signal.h>
  29. #include <linux/slab.h>
  30. #include <linux/types.h>
  31. struct pcie_port_info {
  32. u32 cfg0_size;
  33. u32 cfg1_size;
  34. u32 io_size;
  35. u32 mem_size;
  36. phys_addr_t io_bus_addr;
  37. phys_addr_t mem_bus_addr;
  38. };
  39. struct pcie_port {
  40. struct device *dev;
  41. u8 controller;
  42. u8 root_bus_nr;
  43. void __iomem *dbi_base;
  44. void __iomem *elbi_base;
  45. void __iomem *phy_base;
  46. void __iomem *purple_base;
  47. u64 cfg0_base;
  48. void __iomem *va_cfg0_base;
  49. u64 cfg1_base;
  50. void __iomem *va_cfg1_base;
  51. u64 io_base;
  52. u64 mem_base;
  53. spinlock_t conf_lock;
  54. struct resource cfg;
  55. struct resource io;
  56. struct resource mem;
  57. struct pcie_port_info config;
  58. struct clk *clk;
  59. struct clk *bus_clk;
  60. int irq;
  61. int reset_gpio;
  62. };
  63. /*
  64. * Exynos PCIe IP consists of Synopsys specific part and Exynos
  65. * specific part. Only core block is a Synopsys designware part;
  66. * other parts are Exynos specific.
  67. */
  68. /* Synopsis specific PCIE configuration registers */
  69. #define PCIE_PORT_LINK_CONTROL 0x710
  70. #define PORT_LINK_MODE_MASK (0x3f << 16)
  71. #define PORT_LINK_MODE_4_LANES (0x7 << 16)
  72. #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
  73. #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
  74. #define PORT_LOGIC_LINK_WIDTH_MASK (0x1ff << 8)
  75. #define PORT_LOGIC_LINK_WIDTH_4_LANES (0x7 << 8)
  76. #define PCIE_MSI_ADDR_LO 0x820
  77. #define PCIE_MSI_ADDR_HI 0x824
  78. #define PCIE_MSI_INTR0_ENABLE 0x828
  79. #define PCIE_MSI_INTR0_MASK 0x82C
  80. #define PCIE_MSI_INTR0_STATUS 0x830
  81. #define PCIE_ATU_VIEWPORT 0x900
  82. #define PCIE_ATU_REGION_INBOUND (0x1 << 31)
  83. #define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
  84. #define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
  85. #define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
  86. #define PCIE_ATU_CR1 0x904
  87. #define PCIE_ATU_TYPE_MEM (0x0 << 0)
  88. #define PCIE_ATU_TYPE_IO (0x2 << 0)
  89. #define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
  90. #define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
  91. #define PCIE_ATU_CR2 0x908
  92. #define PCIE_ATU_ENABLE (0x1 << 31)
  93. #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
  94. #define PCIE_ATU_LOWER_BASE 0x90C
  95. #define PCIE_ATU_UPPER_BASE 0x910
  96. #define PCIE_ATU_LIMIT 0x914
  97. #define PCIE_ATU_LOWER_TARGET 0x918
  98. #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
  99. #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
  100. #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
  101. #define PCIE_ATU_UPPER_TARGET 0x91C
  102. /* Exynos specific PCIE configuration registers */
  103. /* PCIe ELBI registers */
  104. #define PCIE_IRQ_PULSE 0x000
  105. #define IRQ_INTA_ASSERT (0x1 << 0)
  106. #define IRQ_INTB_ASSERT (0x1 << 2)
  107. #define IRQ_INTC_ASSERT (0x1 << 4)
  108. #define IRQ_INTD_ASSERT (0x1 << 6)
  109. #define PCIE_IRQ_LEVEL 0x004
  110. #define PCIE_IRQ_SPECIAL 0x008
  111. #define PCIE_IRQ_EN_PULSE 0x00c
  112. #define PCIE_IRQ_EN_LEVEL 0x010
  113. #define PCIE_IRQ_EN_SPECIAL 0x014
  114. #define PCIE_PWR_RESET 0x018
  115. #define PCIE_CORE_RESET 0x01c
  116. #define PCIE_CORE_RESET_ENABLE (0x1 << 0)
  117. #define PCIE_STICKY_RESET 0x020
  118. #define PCIE_NONSTICKY_RESET 0x024
  119. #define PCIE_APP_INIT_RESET 0x028
  120. #define PCIE_APP_LTSSM_ENABLE 0x02c
  121. #define PCIE_ELBI_RDLH_LINKUP 0x064
  122. #define PCIE_ELBI_LTSSM_ENABLE 0x1
  123. #define PCIE_ELBI_SLV_AWMISC 0x11c
  124. #define PCIE_ELBI_SLV_ARMISC 0x120
  125. #define PCIE_ELBI_SLV_DBI_ENABLE (0x1 << 21)
  126. /* PCIe Purple registers */
  127. #define PCIE_PHY_GLOBAL_RESET 0x000
  128. #define PCIE_PHY_COMMON_RESET 0x004
  129. #define PCIE_PHY_CMN_REG 0x008
  130. #define PCIE_PHY_MAC_RESET 0x00c
  131. #define PCIE_PHY_PLL_LOCKED 0x010
  132. #define PCIE_PHY_TRSVREG_RESET 0x020
  133. #define PCIE_PHY_TRSV_RESET 0x024
  134. /* PCIe PHY registers */
  135. #define PCIE_PHY_IMPEDANCE 0x004
  136. #define PCIE_PHY_PLL_DIV_0 0x008
  137. #define PCIE_PHY_PLL_BIAS 0x00c
  138. #define PCIE_PHY_DCC_FEEDBACK 0x014
  139. #define PCIE_PHY_PLL_DIV_1 0x05c
  140. #define PCIE_PHY_TRSV0_EMP_LVL 0x084
  141. #define PCIE_PHY_TRSV0_DRV_LVL 0x088
  142. #define PCIE_PHY_TRSV0_RXCDR 0x0ac
  143. #define PCIE_PHY_TRSV0_LVCC 0x0dc
  144. #define PCIE_PHY_TRSV1_EMP_LVL 0x144
  145. #define PCIE_PHY_TRSV1_RXCDR 0x16c
  146. #define PCIE_PHY_TRSV1_LVCC 0x19c
  147. #define PCIE_PHY_TRSV2_EMP_LVL 0x204
  148. #define PCIE_PHY_TRSV2_RXCDR 0x22c
  149. #define PCIE_PHY_TRSV2_LVCC 0x25c
  150. #define PCIE_PHY_TRSV3_EMP_LVL 0x2c4
  151. #define PCIE_PHY_TRSV3_RXCDR 0x2ec
  152. #define PCIE_PHY_TRSV3_LVCC 0x31c
  153. static struct hw_pci exynos_pci;
  154. static inline struct pcie_port *sys_to_pcie(struct pci_sys_data *sys)
  155. {
  156. return sys->private_data;
  157. }
  158. static inline int cfg_read(void *addr, int where, int size, u32 *val)
  159. {
  160. *val = readl(addr);
  161. if (size == 1)
  162. *val = (*val >> (8 * (where & 3))) & 0xff;
  163. else if (size == 2)
  164. *val = (*val >> (8 * (where & 3))) & 0xffff;
  165. else if (size != 4)
  166. return PCIBIOS_BAD_REGISTER_NUMBER;
  167. return PCIBIOS_SUCCESSFUL;
  168. }
  169. static inline int cfg_write(void *addr, int where, int size, u32 val)
  170. {
  171. if (size == 4)
  172. writel(val, addr);
  173. else if (size == 2)
  174. writew(val, addr + (where & 2));
  175. else if (size == 1)
  176. writeb(val, addr + (where & 3));
  177. else
  178. return PCIBIOS_BAD_REGISTER_NUMBER;
  179. return PCIBIOS_SUCCESSFUL;
  180. }
  181. static void exynos_pcie_sideband_dbi_w_mode(struct pcie_port *pp, bool on)
  182. {
  183. u32 val;
  184. if (on) {
  185. val = readl(pp->elbi_base + PCIE_ELBI_SLV_AWMISC);
  186. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  187. writel(val, pp->elbi_base + PCIE_ELBI_SLV_AWMISC);
  188. } else {
  189. val = readl(pp->elbi_base + PCIE_ELBI_SLV_AWMISC);
  190. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  191. writel(val, pp->elbi_base + PCIE_ELBI_SLV_AWMISC);
  192. }
  193. }
  194. static void exynos_pcie_sideband_dbi_r_mode(struct pcie_port *pp, bool on)
  195. {
  196. u32 val;
  197. if (on) {
  198. val = readl(pp->elbi_base + PCIE_ELBI_SLV_ARMISC);
  199. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  200. writel(val, pp->elbi_base + PCIE_ELBI_SLV_ARMISC);
  201. } else {
  202. val = readl(pp->elbi_base + PCIE_ELBI_SLV_ARMISC);
  203. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  204. writel(val, pp->elbi_base + PCIE_ELBI_SLV_ARMISC);
  205. }
  206. }
  207. static inline void readl_rc(struct pcie_port *pp, void *dbi_base, u32 *val)
  208. {
  209. exynos_pcie_sideband_dbi_r_mode(pp, true);
  210. *val = readl(dbi_base);
  211. exynos_pcie_sideband_dbi_r_mode(pp, false);
  212. return;
  213. }
  214. static inline void writel_rc(struct pcie_port *pp, u32 val, void *dbi_base)
  215. {
  216. exynos_pcie_sideband_dbi_w_mode(pp, true);
  217. writel(val, dbi_base);
  218. exynos_pcie_sideband_dbi_w_mode(pp, false);
  219. return;
  220. }
  221. static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
  222. u32 *val)
  223. {
  224. int ret;
  225. exynos_pcie_sideband_dbi_r_mode(pp, true);
  226. ret = cfg_read(pp->dbi_base + (where & ~0x3), where, size, val);
  227. exynos_pcie_sideband_dbi_r_mode(pp, false);
  228. return ret;
  229. }
  230. static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
  231. u32 val)
  232. {
  233. int ret;
  234. exynos_pcie_sideband_dbi_w_mode(pp, true);
  235. ret = cfg_write(pp->dbi_base + (where & ~0x3), where, size, val);
  236. exynos_pcie_sideband_dbi_w_mode(pp, false);
  237. return ret;
  238. }
  239. static void exynos_pcie_prog_viewport_cfg0(struct pcie_port *pp, u32 busdev)
  240. {
  241. u32 val;
  242. void __iomem *dbi_base = pp->dbi_base;
  243. /* Program viewport 0 : OUTBOUND : CFG0 */
  244. val = PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0;
  245. writel_rc(pp, val, dbi_base + PCIE_ATU_VIEWPORT);
  246. writel_rc(pp, pp->cfg0_base, dbi_base + PCIE_ATU_LOWER_BASE);
  247. writel_rc(pp, (pp->cfg0_base >> 32), dbi_base + PCIE_ATU_UPPER_BASE);
  248. writel_rc(pp, pp->cfg0_base + pp->config.cfg0_size - 1,
  249. dbi_base + PCIE_ATU_LIMIT);
  250. writel_rc(pp, busdev, dbi_base + PCIE_ATU_LOWER_TARGET);
  251. writel_rc(pp, 0, dbi_base + PCIE_ATU_UPPER_TARGET);
  252. writel_rc(pp, PCIE_ATU_TYPE_CFG0, dbi_base + PCIE_ATU_CR1);
  253. val = PCIE_ATU_ENABLE;
  254. writel_rc(pp, val, dbi_base + PCIE_ATU_CR2);
  255. }
  256. static void exynos_pcie_prog_viewport_cfg1(struct pcie_port *pp, u32 busdev)
  257. {
  258. u32 val;
  259. void __iomem *dbi_base = pp->dbi_base;
  260. /* Program viewport 1 : OUTBOUND : CFG1 */
  261. val = PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1;
  262. writel_rc(pp, val, dbi_base + PCIE_ATU_VIEWPORT);
  263. writel_rc(pp, PCIE_ATU_TYPE_CFG1, dbi_base + PCIE_ATU_CR1);
  264. val = PCIE_ATU_ENABLE;
  265. writel_rc(pp, val, dbi_base + PCIE_ATU_CR2);
  266. writel_rc(pp, pp->cfg1_base, dbi_base + PCIE_ATU_LOWER_BASE);
  267. writel_rc(pp, (pp->cfg1_base >> 32), dbi_base + PCIE_ATU_UPPER_BASE);
  268. writel_rc(pp, pp->cfg1_base + pp->config.cfg1_size - 1,
  269. dbi_base + PCIE_ATU_LIMIT);
  270. writel_rc(pp, busdev, dbi_base + PCIE_ATU_LOWER_TARGET);
  271. writel_rc(pp, 0, dbi_base + PCIE_ATU_UPPER_TARGET);
  272. }
  273. static void exynos_pcie_prog_viewport_mem_outbound(struct pcie_port *pp)
  274. {
  275. u32 val;
  276. void __iomem *dbi_base = pp->dbi_base;
  277. /* Program viewport 0 : OUTBOUND : MEM */
  278. val = PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0;
  279. writel_rc(pp, val, dbi_base + PCIE_ATU_VIEWPORT);
  280. writel_rc(pp, PCIE_ATU_TYPE_MEM, dbi_base + PCIE_ATU_CR1);
  281. val = PCIE_ATU_ENABLE;
  282. writel_rc(pp, val, dbi_base + PCIE_ATU_CR2);
  283. writel_rc(pp, pp->mem_base, dbi_base + PCIE_ATU_LOWER_BASE);
  284. writel_rc(pp, (pp->mem_base >> 32), dbi_base + PCIE_ATU_UPPER_BASE);
  285. writel_rc(pp, pp->mem_base + pp->config.mem_size - 1,
  286. dbi_base + PCIE_ATU_LIMIT);
  287. writel_rc(pp, pp->config.mem_bus_addr,
  288. dbi_base + PCIE_ATU_LOWER_TARGET);
  289. writel_rc(pp, upper_32_bits(pp->config.mem_bus_addr),
  290. dbi_base + PCIE_ATU_UPPER_TARGET);
  291. }
  292. static void exynos_pcie_prog_viewport_io_outbound(struct pcie_port *pp)
  293. {
  294. u32 val;
  295. void __iomem *dbi_base = pp->dbi_base;
  296. /* Program viewport 1 : OUTBOUND : IO */
  297. val = PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1;
  298. writel_rc(pp, val, dbi_base + PCIE_ATU_VIEWPORT);
  299. writel_rc(pp, PCIE_ATU_TYPE_IO, dbi_base + PCIE_ATU_CR1);
  300. val = PCIE_ATU_ENABLE;
  301. writel_rc(pp, val, dbi_base + PCIE_ATU_CR2);
  302. writel_rc(pp, pp->io_base, dbi_base + PCIE_ATU_LOWER_BASE);
  303. writel_rc(pp, (pp->io_base >> 32), dbi_base + PCIE_ATU_UPPER_BASE);
  304. writel_rc(pp, pp->io_base + pp->config.io_size - 1,
  305. dbi_base + PCIE_ATU_LIMIT);
  306. writel_rc(pp, pp->config.io_bus_addr,
  307. dbi_base + PCIE_ATU_LOWER_TARGET);
  308. writel_rc(pp, upper_32_bits(pp->config.io_bus_addr),
  309. dbi_base + PCIE_ATU_UPPER_TARGET);
  310. }
  311. static int exynos_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  312. u32 devfn, int where, int size, u32 *val)
  313. {
  314. int ret = PCIBIOS_SUCCESSFUL;
  315. u32 address, busdev;
  316. busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
  317. PCIE_ATU_FUNC(PCI_FUNC(devfn));
  318. address = where & ~0x3;
  319. if (bus->parent->number == pp->root_bus_nr) {
  320. exynos_pcie_prog_viewport_cfg0(pp, busdev);
  321. ret = cfg_read(pp->va_cfg0_base + address, where, size, val);
  322. exynos_pcie_prog_viewport_mem_outbound(pp);
  323. } else {
  324. exynos_pcie_prog_viewport_cfg1(pp, busdev);
  325. ret = cfg_read(pp->va_cfg1_base + address, where, size, val);
  326. exynos_pcie_prog_viewport_io_outbound(pp);
  327. }
  328. return ret;
  329. }
  330. static int exynos_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  331. u32 devfn, int where, int size, u32 val)
  332. {
  333. int ret = PCIBIOS_SUCCESSFUL;
  334. u32 address, busdev;
  335. busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
  336. PCIE_ATU_FUNC(PCI_FUNC(devfn));
  337. address = where & ~0x3;
  338. if (bus->parent->number == pp->root_bus_nr) {
  339. exynos_pcie_prog_viewport_cfg0(pp, busdev);
  340. ret = cfg_write(pp->va_cfg0_base + address, where, size, val);
  341. exynos_pcie_prog_viewport_mem_outbound(pp);
  342. } else {
  343. exynos_pcie_prog_viewport_cfg1(pp, busdev);
  344. ret = cfg_write(pp->va_cfg1_base + address, where, size, val);
  345. exynos_pcie_prog_viewport_io_outbound(pp);
  346. }
  347. return ret;
  348. }
  349. static unsigned long global_io_offset;
  350. static int exynos_pcie_setup(int nr, struct pci_sys_data *sys)
  351. {
  352. struct pcie_port *pp;
  353. pp = sys_to_pcie(sys);
  354. if (!pp)
  355. return 0;
  356. if (global_io_offset < SZ_1M && pp->config.io_size > 0) {
  357. sys->io_offset = global_io_offset - pp->config.io_bus_addr;
  358. pci_ioremap_io(sys->io_offset, pp->io.start);
  359. global_io_offset += SZ_64K;
  360. pci_add_resource_offset(&sys->resources, &pp->io,
  361. sys->io_offset);
  362. }
  363. sys->mem_offset = pp->mem.start - pp->config.mem_bus_addr;
  364. pci_add_resource_offset(&sys->resources, &pp->mem, sys->mem_offset);
  365. return 1;
  366. }
  367. static int exynos_pcie_link_up(struct pcie_port *pp)
  368. {
  369. u32 val = readl(pp->elbi_base + PCIE_ELBI_RDLH_LINKUP);
  370. if (val == PCIE_ELBI_LTSSM_ENABLE)
  371. return 1;
  372. return 0;
  373. }
  374. static int exynos_pcie_valid_config(struct pcie_port *pp,
  375. struct pci_bus *bus, int dev)
  376. {
  377. /* If there is no link, then there is no device */
  378. if (bus->number != pp->root_bus_nr) {
  379. if (!exynos_pcie_link_up(pp))
  380. return 0;
  381. }
  382. /* access only one slot on each root port */
  383. if (bus->number == pp->root_bus_nr && dev > 0)
  384. return 0;
  385. /*
  386. * do not read more than one device on the bus directly attached
  387. * to RC's (Virtual Bridge's) DS side.
  388. */
  389. if (bus->primary == pp->root_bus_nr && dev > 0)
  390. return 0;
  391. return 1;
  392. }
  393. static int exynos_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  394. int size, u32 *val)
  395. {
  396. struct pcie_port *pp = sys_to_pcie(bus->sysdata);
  397. unsigned long flags;
  398. int ret;
  399. if (!pp) {
  400. BUG();
  401. return -EINVAL;
  402. }
  403. if (exynos_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0) {
  404. *val = 0xffffffff;
  405. return PCIBIOS_DEVICE_NOT_FOUND;
  406. }
  407. spin_lock_irqsave(&pp->conf_lock, flags);
  408. if (bus->number != pp->root_bus_nr)
  409. ret = exynos_pcie_rd_other_conf(pp, bus, devfn,
  410. where, size, val);
  411. else
  412. ret = exynos_pcie_rd_own_conf(pp, where, size, val);
  413. spin_unlock_irqrestore(&pp->conf_lock, flags);
  414. return ret;
  415. }
  416. static int exynos_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  417. int where, int size, u32 val)
  418. {
  419. struct pcie_port *pp = sys_to_pcie(bus->sysdata);
  420. unsigned long flags;
  421. int ret;
  422. if (!pp) {
  423. BUG();
  424. return -EINVAL;
  425. }
  426. if (exynos_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0)
  427. return PCIBIOS_DEVICE_NOT_FOUND;
  428. spin_lock_irqsave(&pp->conf_lock, flags);
  429. if (bus->number != pp->root_bus_nr)
  430. ret = exynos_pcie_wr_other_conf(pp, bus, devfn,
  431. where, size, val);
  432. else
  433. ret = exynos_pcie_wr_own_conf(pp, where, size, val);
  434. spin_unlock_irqrestore(&pp->conf_lock, flags);
  435. return ret;
  436. }
  437. static struct pci_ops exynos_pcie_ops = {
  438. .read = exynos_pcie_rd_conf,
  439. .write = exynos_pcie_wr_conf,
  440. };
  441. static struct pci_bus *exynos_pcie_scan_bus(int nr,
  442. struct pci_sys_data *sys)
  443. {
  444. struct pci_bus *bus;
  445. struct pcie_port *pp = sys_to_pcie(sys);
  446. if (pp) {
  447. pp->root_bus_nr = sys->busnr;
  448. bus = pci_scan_root_bus(NULL, sys->busnr, &exynos_pcie_ops,
  449. sys, &sys->resources);
  450. } else {
  451. bus = NULL;
  452. BUG();
  453. }
  454. return bus;
  455. }
  456. static int exynos_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  457. {
  458. struct pcie_port *pp = sys_to_pcie(dev->bus->sysdata);
  459. return pp->irq;
  460. }
  461. static struct hw_pci exynos_pci = {
  462. .setup = exynos_pcie_setup,
  463. .scan = exynos_pcie_scan_bus,
  464. .map_irq = exynos_pcie_map_irq,
  465. };
  466. static void exynos_pcie_setup_rc(struct pcie_port *pp)
  467. {
  468. struct pcie_port_info *config = &pp->config;
  469. void __iomem *dbi_base = pp->dbi_base;
  470. u32 val;
  471. u32 membase;
  472. u32 memlimit;
  473. /* set the number of lines as 4 */
  474. readl_rc(pp, dbi_base + PCIE_PORT_LINK_CONTROL, &val);
  475. val &= ~PORT_LINK_MODE_MASK;
  476. val |= PORT_LINK_MODE_4_LANES;
  477. writel_rc(pp, val, dbi_base + PCIE_PORT_LINK_CONTROL);
  478. /* set link width speed control register */
  479. readl_rc(pp, dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL, &val);
  480. val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
  481. val |= PORT_LOGIC_LINK_WIDTH_4_LANES;
  482. writel_rc(pp, val, dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
  483. /* setup RC BARs */
  484. writel_rc(pp, 0x00000004, dbi_base + PCI_BASE_ADDRESS_0);
  485. writel_rc(pp, 0x00000004, dbi_base + PCI_BASE_ADDRESS_1);
  486. /* setup interrupt pins */
  487. readl_rc(pp, dbi_base + PCI_INTERRUPT_LINE, &val);
  488. val &= 0xffff00ff;
  489. val |= 0x00000100;
  490. writel_rc(pp, val, dbi_base + PCI_INTERRUPT_LINE);
  491. /* setup bus numbers */
  492. readl_rc(pp, dbi_base + PCI_PRIMARY_BUS, &val);
  493. val &= 0xff000000;
  494. val |= 0x00010100;
  495. writel_rc(pp, val, dbi_base + PCI_PRIMARY_BUS);
  496. /* setup memory base, memory limit */
  497. membase = ((u32)pp->mem_base & 0xfff00000) >> 16;
  498. memlimit = (config->mem_size + (u32)pp->mem_base) & 0xfff00000;
  499. val = memlimit | membase;
  500. writel_rc(pp, val, dbi_base + PCI_MEMORY_BASE);
  501. /* setup command register */
  502. readl_rc(pp, dbi_base + PCI_COMMAND, &val);
  503. val &= 0xffff0000;
  504. val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  505. PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
  506. writel_rc(pp, val, dbi_base + PCI_COMMAND);
  507. }
  508. static void exynos_pcie_assert_core_reset(struct pcie_port *pp)
  509. {
  510. u32 val;
  511. void __iomem *elbi_base = pp->elbi_base;
  512. val = readl(elbi_base + PCIE_CORE_RESET);
  513. val &= ~PCIE_CORE_RESET_ENABLE;
  514. writel(val, elbi_base + PCIE_CORE_RESET);
  515. writel(0, elbi_base + PCIE_PWR_RESET);
  516. writel(0, elbi_base + PCIE_STICKY_RESET);
  517. writel(0, elbi_base + PCIE_NONSTICKY_RESET);
  518. }
  519. static void exynos_pcie_deassert_core_reset(struct pcie_port *pp)
  520. {
  521. u32 val;
  522. void __iomem *elbi_base = pp->elbi_base;
  523. void __iomem *purple_base = pp->purple_base;
  524. val = readl(elbi_base + PCIE_CORE_RESET);
  525. val |= PCIE_CORE_RESET_ENABLE;
  526. writel(val, elbi_base + PCIE_CORE_RESET);
  527. writel(1, elbi_base + PCIE_STICKY_RESET);
  528. writel(1, elbi_base + PCIE_NONSTICKY_RESET);
  529. writel(1, elbi_base + PCIE_APP_INIT_RESET);
  530. writel(0, elbi_base + PCIE_APP_INIT_RESET);
  531. writel(1, purple_base + PCIE_PHY_MAC_RESET);
  532. }
  533. static void exynos_pcie_assert_phy_reset(struct pcie_port *pp)
  534. {
  535. void __iomem *purple_base = pp->purple_base;
  536. writel(0, purple_base + PCIE_PHY_MAC_RESET);
  537. writel(1, purple_base + PCIE_PHY_GLOBAL_RESET);
  538. }
  539. static void exynos_pcie_deassert_phy_reset(struct pcie_port *pp)
  540. {
  541. void __iomem *elbi_base = pp->elbi_base;
  542. void __iomem *purple_base = pp->purple_base;
  543. writel(0, purple_base + PCIE_PHY_GLOBAL_RESET);
  544. writel(1, elbi_base + PCIE_PWR_RESET);
  545. writel(0, purple_base + PCIE_PHY_COMMON_RESET);
  546. writel(0, purple_base + PCIE_PHY_CMN_REG);
  547. writel(0, purple_base + PCIE_PHY_TRSVREG_RESET);
  548. writel(0, purple_base + PCIE_PHY_TRSV_RESET);
  549. }
  550. static void exynos_pcie_init_phy(struct pcie_port *pp)
  551. {
  552. void __iomem *phy_base = pp->phy_base;
  553. /* DCC feedback control off */
  554. writel(0x29, phy_base + PCIE_PHY_DCC_FEEDBACK);
  555. /* set TX/RX impedance */
  556. writel(0xd5, phy_base + PCIE_PHY_IMPEDANCE);
  557. /* set 50Mhz PHY clock */
  558. writel(0x14, phy_base + PCIE_PHY_PLL_DIV_0);
  559. writel(0x12, phy_base + PCIE_PHY_PLL_DIV_1);
  560. /* set TX Differential output for lane 0 */
  561. writel(0x7f, phy_base + PCIE_PHY_TRSV0_DRV_LVL);
  562. /* set TX Pre-emphasis Level Control for lane 0 to minimum */
  563. writel(0x0, phy_base + PCIE_PHY_TRSV0_EMP_LVL);
  564. /* set RX clock and data recovery bandwidth */
  565. writel(0xe7, phy_base + PCIE_PHY_PLL_BIAS);
  566. writel(0x82, phy_base + PCIE_PHY_TRSV0_RXCDR);
  567. writel(0x82, phy_base + PCIE_PHY_TRSV1_RXCDR);
  568. writel(0x82, phy_base + PCIE_PHY_TRSV2_RXCDR);
  569. writel(0x82, phy_base + PCIE_PHY_TRSV3_RXCDR);
  570. /* change TX Pre-emphasis Level Control for lanes */
  571. writel(0x39, phy_base + PCIE_PHY_TRSV0_EMP_LVL);
  572. writel(0x39, phy_base + PCIE_PHY_TRSV1_EMP_LVL);
  573. writel(0x39, phy_base + PCIE_PHY_TRSV2_EMP_LVL);
  574. writel(0x39, phy_base + PCIE_PHY_TRSV3_EMP_LVL);
  575. /* set LVCC */
  576. writel(0x20, phy_base + PCIE_PHY_TRSV0_LVCC);
  577. writel(0xa0, phy_base + PCIE_PHY_TRSV1_LVCC);
  578. writel(0xa0, phy_base + PCIE_PHY_TRSV2_LVCC);
  579. writel(0xa0, phy_base + PCIE_PHY_TRSV3_LVCC);
  580. }
  581. static void exynos_pcie_assert_reset(struct pcie_port *pp)
  582. {
  583. if (pp->reset_gpio >= 0)
  584. devm_gpio_request_one(pp->dev, pp->reset_gpio,
  585. GPIOF_OUT_INIT_HIGH, "RESET");
  586. return;
  587. }
  588. static int exynos_pcie_establish_link(struct pcie_port *pp)
  589. {
  590. u32 val;
  591. int count = 0;
  592. void __iomem *elbi_base = pp->elbi_base;
  593. void __iomem *purple_base = pp->purple_base;
  594. void __iomem *phy_base = pp->phy_base;
  595. if (exynos_pcie_link_up(pp)) {
  596. dev_err(pp->dev, "Link already up\n");
  597. return 0;
  598. }
  599. /* assert reset signals */
  600. exynos_pcie_assert_core_reset(pp);
  601. exynos_pcie_assert_phy_reset(pp);
  602. /* de-assert phy reset */
  603. exynos_pcie_deassert_phy_reset(pp);
  604. /* initialize phy */
  605. exynos_pcie_init_phy(pp);
  606. /* pulse for common reset */
  607. writel(1, purple_base + PCIE_PHY_COMMON_RESET);
  608. udelay(500);
  609. writel(0, purple_base + PCIE_PHY_COMMON_RESET);
  610. /* de-assert core reset */
  611. exynos_pcie_deassert_core_reset(pp);
  612. /* setup root complex */
  613. exynos_pcie_setup_rc(pp);
  614. /* assert reset signal */
  615. exynos_pcie_assert_reset(pp);
  616. /* assert LTSSM enable */
  617. writel(PCIE_ELBI_LTSSM_ENABLE, elbi_base + PCIE_APP_LTSSM_ENABLE);
  618. /* check if the link is up or not */
  619. while (!exynos_pcie_link_up(pp)) {
  620. mdelay(100);
  621. count++;
  622. if (count == 10) {
  623. while (readl(phy_base + PCIE_PHY_PLL_LOCKED) == 0) {
  624. val = readl(purple_base + PCIE_PHY_PLL_LOCKED);
  625. dev_info(pp->dev, "PLL Locked: 0x%x\n", val);
  626. }
  627. dev_err(pp->dev, "PCIe Link Fail\n");
  628. return -EINVAL;
  629. }
  630. }
  631. dev_info(pp->dev, "Link up\n");
  632. return 0;
  633. }
  634. static void exynos_pcie_clear_irq_pulse(struct pcie_port *pp)
  635. {
  636. u32 val;
  637. void __iomem *elbi_base = pp->elbi_base;
  638. val = readl(elbi_base + PCIE_IRQ_PULSE);
  639. writel(val, elbi_base + PCIE_IRQ_PULSE);
  640. return;
  641. }
  642. static void exynos_pcie_enable_irq_pulse(struct pcie_port *pp)
  643. {
  644. u32 val;
  645. void __iomem *elbi_base = pp->elbi_base;
  646. /* enable INTX interrupt */
  647. val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
  648. IRQ_INTC_ASSERT | IRQ_INTD_ASSERT,
  649. writel(val, elbi_base + PCIE_IRQ_EN_PULSE);
  650. return;
  651. }
  652. static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
  653. {
  654. struct pcie_port *pp = arg;
  655. exynos_pcie_clear_irq_pulse(pp);
  656. return IRQ_HANDLED;
  657. }
  658. static void exynos_pcie_enable_interrupts(struct pcie_port *pp)
  659. {
  660. exynos_pcie_enable_irq_pulse(pp);
  661. return;
  662. }
  663. static void exynos_pcie_host_init(struct pcie_port *pp)
  664. {
  665. struct pcie_port_info *config = &pp->config;
  666. u32 val;
  667. /* Keep first 64K for IO */
  668. pp->cfg0_base = pp->cfg.start;
  669. pp->cfg1_base = pp->cfg.start + config->cfg0_size;
  670. pp->io_base = pp->io.start;
  671. pp->mem_base = pp->mem.start;
  672. /* enable link */
  673. exynos_pcie_establish_link(pp);
  674. exynos_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
  675. /* program correct class for RC */
  676. exynos_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI);
  677. exynos_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val);
  678. val |= PORT_LOGIC_SPEED_CHANGE;
  679. exynos_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
  680. exynos_pcie_enable_interrupts(pp);
  681. }
  682. static int add_pcie_port(struct pcie_port *pp, struct platform_device *pdev)
  683. {
  684. struct resource *elbi_base;
  685. struct resource *phy_base;
  686. struct resource *purple_base;
  687. int ret;
  688. elbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  689. if (!elbi_base) {
  690. dev_err(&pdev->dev, "couldn't get elbi base resource\n");
  691. return -EINVAL;
  692. }
  693. pp->elbi_base = devm_ioremap_resource(&pdev->dev, elbi_base);
  694. if (IS_ERR(pp->elbi_base))
  695. return PTR_ERR(pp->elbi_base);
  696. phy_base = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  697. if (!phy_base) {
  698. dev_err(&pdev->dev, "couldn't get phy base resource\n");
  699. return -EINVAL;
  700. }
  701. pp->phy_base = devm_ioremap_resource(&pdev->dev, phy_base);
  702. if (IS_ERR(pp->phy_base))
  703. return PTR_ERR(pp->phy_base);
  704. purple_base = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  705. if (!purple_base) {
  706. dev_err(&pdev->dev, "couldn't get purple base resource\n");
  707. return -EINVAL;
  708. }
  709. pp->purple_base = devm_ioremap_resource(&pdev->dev, purple_base);
  710. if (IS_ERR(pp->purple_base))
  711. return PTR_ERR(pp->purple_base);
  712. pp->irq = platform_get_irq(pdev, 1);
  713. if (!pp->irq) {
  714. dev_err(&pdev->dev, "failed to get irq\n");
  715. return -ENODEV;
  716. }
  717. ret = devm_request_irq(&pdev->dev, pp->irq, exynos_pcie_irq_handler,
  718. IRQF_SHARED, "exynos-pcie", pp);
  719. if (ret) {
  720. dev_err(&pdev->dev, "failed to request irq\n");
  721. return ret;
  722. }
  723. pp->dbi_base = devm_ioremap(&pdev->dev, pp->cfg.start,
  724. resource_size(&pp->cfg));
  725. if (!pp->dbi_base) {
  726. dev_err(&pdev->dev, "error with ioremap\n");
  727. return -ENOMEM;
  728. }
  729. pp->root_bus_nr = -1;
  730. spin_lock_init(&pp->conf_lock);
  731. exynos_pcie_host_init(pp);
  732. pp->va_cfg0_base = devm_ioremap(&pdev->dev, pp->cfg0_base,
  733. pp->config.cfg0_size);
  734. if (!pp->va_cfg0_base) {
  735. dev_err(pp->dev, "error with ioremap in function\n");
  736. return -ENOMEM;
  737. }
  738. pp->va_cfg1_base = devm_ioremap(&pdev->dev, pp->cfg1_base,
  739. pp->config.cfg1_size);
  740. if (!pp->va_cfg1_base) {
  741. dev_err(pp->dev, "error with ioremap\n");
  742. return -ENOMEM;
  743. }
  744. return 0;
  745. }
  746. static int __init exynos_pcie_probe(struct platform_device *pdev)
  747. {
  748. struct pcie_port *pp;
  749. struct device_node *np = pdev->dev.of_node;
  750. struct of_pci_range range;
  751. struct of_pci_range_parser parser;
  752. int ret;
  753. pp = devm_kzalloc(&pdev->dev, sizeof(*pp), GFP_KERNEL);
  754. if (!pp) {
  755. dev_err(&pdev->dev, "no memory for pcie port\n");
  756. return -ENOMEM;
  757. }
  758. pp->dev = &pdev->dev;
  759. if (of_pci_range_parser_init(&parser, np)) {
  760. dev_err(&pdev->dev, "missing ranges property\n");
  761. return -EINVAL;
  762. }
  763. /* Get the I/O and memory ranges from DT */
  764. for_each_of_pci_range(&parser, &range) {
  765. unsigned long restype = range.flags & IORESOURCE_TYPE_BITS;
  766. if (restype == IORESOURCE_IO) {
  767. of_pci_range_to_resource(&range, np, &pp->io);
  768. pp->io.name = "I/O";
  769. pp->io.start = max_t(resource_size_t,
  770. PCIBIOS_MIN_IO,
  771. range.pci_addr + global_io_offset);
  772. pp->io.end = min_t(resource_size_t,
  773. IO_SPACE_LIMIT,
  774. range.pci_addr + range.size
  775. + global_io_offset);
  776. pp->config.io_size = resource_size(&pp->io);
  777. pp->config.io_bus_addr = range.pci_addr;
  778. }
  779. if (restype == IORESOURCE_MEM) {
  780. of_pci_range_to_resource(&range, np, &pp->mem);
  781. pp->mem.name = "MEM";
  782. pp->config.mem_size = resource_size(&pp->mem);
  783. pp->config.mem_bus_addr = range.pci_addr;
  784. }
  785. if (restype == 0) {
  786. of_pci_range_to_resource(&range, np, &pp->cfg);
  787. pp->config.cfg0_size = resource_size(&pp->cfg)/2;
  788. pp->config.cfg1_size = resource_size(&pp->cfg)/2;
  789. }
  790. }
  791. pp->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
  792. pp->clk = devm_clk_get(&pdev->dev, "pcie");
  793. if (IS_ERR(pp->clk)) {
  794. dev_err(&pdev->dev, "Failed to get pcie rc clock\n");
  795. return PTR_ERR(pp->clk);
  796. }
  797. ret = clk_prepare_enable(pp->clk);
  798. if (ret)
  799. return ret;
  800. pp->bus_clk = devm_clk_get(&pdev->dev, "pcie_bus");
  801. if (IS_ERR(pp->bus_clk)) {
  802. dev_err(&pdev->dev, "Failed to get pcie bus clock\n");
  803. ret = PTR_ERR(pp->bus_clk);
  804. goto fail_clk;
  805. }
  806. ret = clk_prepare_enable(pp->bus_clk);
  807. if (ret)
  808. goto fail_clk;
  809. ret = add_pcie_port(pp, pdev);
  810. if (ret < 0)
  811. goto fail_bus_clk;
  812. pp->controller = exynos_pci.nr_controllers;
  813. exynos_pci.nr_controllers = 1;
  814. exynos_pci.private_data = (void **)&pp;
  815. pci_common_init(&exynos_pci);
  816. pci_assign_unassigned_resources();
  817. #ifdef CONFIG_PCI_DOMAINS
  818. exynos_pci.domain++;
  819. #endif
  820. platform_set_drvdata(pdev, pp);
  821. return 0;
  822. fail_bus_clk:
  823. clk_disable_unprepare(pp->bus_clk);
  824. fail_clk:
  825. clk_disable_unprepare(pp->clk);
  826. return ret;
  827. }
  828. static int __exit exynos_pcie_remove(struct platform_device *pdev)
  829. {
  830. struct pcie_port *pp = platform_get_drvdata(pdev);
  831. clk_disable_unprepare(pp->bus_clk);
  832. clk_disable_unprepare(pp->clk);
  833. return 0;
  834. }
  835. static const struct of_device_id exynos_pcie_of_match[] = {
  836. { .compatible = "samsung,exynos5440-pcie", },
  837. {},
  838. };
  839. MODULE_DEVICE_TABLE(of, exynos_pcie_of_match);
  840. static struct platform_driver exynos_pcie_driver = {
  841. .remove = __exit_p(exynos_pcie_remove),
  842. .driver = {
  843. .name = "exynos-pcie",
  844. .owner = THIS_MODULE,
  845. .of_match_table = of_match_ptr(exynos_pcie_of_match),
  846. },
  847. };
  848. static int exynos_pcie_abort(unsigned long addr, unsigned int fsr,
  849. struct pt_regs *regs)
  850. {
  851. unsigned long pc = instruction_pointer(regs);
  852. unsigned long instr = *(unsigned long *)pc;
  853. WARN_ONCE(1, "pcie abort\n");
  854. /*
  855. * If the instruction being executed was a read,
  856. * make it look like it read all-ones.
  857. */
  858. if ((instr & 0x0c100000) == 0x04100000) {
  859. int reg = (instr >> 12) & 15;
  860. unsigned long val;
  861. if (instr & 0x00400000)
  862. val = 255;
  863. else
  864. val = -1;
  865. regs->uregs[reg] = val;
  866. regs->ARM_pc += 4;
  867. return 0;
  868. }
  869. if ((instr & 0x0e100090) == 0x00100090) {
  870. int reg = (instr >> 12) & 15;
  871. regs->uregs[reg] = -1;
  872. regs->ARM_pc += 4;
  873. return 0;
  874. }
  875. return 1;
  876. }
  877. /* Exynos PCIe driver does not allow module unload */
  878. static int __init pcie_init(void)
  879. {
  880. hook_fault_code(16 + 6, exynos_pcie_abort, SIGBUS, 0,
  881. "imprecise external abort");
  882. platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
  883. return 0;
  884. }
  885. subsys_initcall(pcie_init);
  886. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
  887. MODULE_DESCRIPTION("Samsung PCIe host controller driver");
  888. MODULE_LICENSE("GPL v2");