170-pcie2-bcma-add-new-PCIe2-driver-for-bcma.patch 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. From cc2cda651fcbc498bf513a6b802dca19944bcb37 Mon Sep 17 00:00:00 2001
  2. From: Hauke Mehrtens <[email protected]>
  3. Date: Mon, 12 May 2014 11:55:20 +0200
  4. Subject: [PATCH 13/17] pcie2-bcma: add new PCIe2 driver for bcma
  5. This driver supports the PCIe controller found on the BCM4708 and
  6. similar SoCs. The controller itself is automatically detected by bcma.
  7. Signed-off-by: Hauke Mehrtens <[email protected]>
  8. ---
  9. arch/arm/mach-bcm/Kconfig | 2 +
  10. drivers/pci/host/Kconfig | 7 +
  11. drivers/pci/host/Makefile | 1 +
  12. drivers/pci/host/pcie2-bcma.c | 591 ++++++++++++++++++++++++++++++++++++++++++
  13. 4 files changed, 601 insertions(+)
  14. create mode 100644 drivers/pci/host/pcie2-bcma.c
  15. --- a/arch/arm/mach-bcm/Kconfig
  16. +++ b/arch/arm/mach-bcm/Kconfig
  17. @@ -45,6 +45,7 @@ config ARCH_BCM_5301X
  18. select ARM_GLOBAL_TIMER
  19. select CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
  20. select MIGHT_HAVE_PCI
  21. + select PCI_DOMAINS if PCI
  22. help
  23. Support for Broadcom BCM470X and BCM5301X SoCs with ARM CPU cores.
  24. --- a/drivers/pci/host/Kconfig
  25. +++ b/drivers/pci/host/Kconfig
  26. @@ -33,4 +33,11 @@ config PCI_RCAR_GEN2
  27. There are 3 internal PCI controllers available with a single
  28. built-in EHCI/OHCI host controller present on each one.
  29. +config PCI_BCMA
  30. + bool "BCMA PCIe2 host controller"
  31. + depends on BCMA && OF
  32. + help
  33. + Say Y here if you want to support a simple generic PCI host
  34. + controller, such as the one emulated by kvmtool.
  35. +
  36. endmenu
  37. --- a/drivers/pci/host/Makefile
  38. +++ b/drivers/pci/host/Makefile
  39. @@ -4,3 +4,4 @@ obj-$(CONFIG_PCI_IMX6) += pci-imx6.o
  40. obj-$(CONFIG_PCI_MVEBU) += pci-mvebu.o
  41. obj-$(CONFIG_PCI_TEGRA) += pci-tegra.o
  42. obj-$(CONFIG_PCI_RCAR_GEN2) += pci-rcar-gen2.o
  43. +obj-$(CONFIG_PCI_BCMA) += pcie2-bcma.o
  44. --- /dev/null
  45. +++ b/drivers/pci/host/pcie2-bcma.c
  46. @@ -0,0 +1,591 @@
  47. +/*
  48. + * Northstar PCI-Express driver
  49. + * Only supports Root-Complex (RC) mode
  50. + *
  51. + * Notes:
  52. + * PCI Domains are being used to identify the PCIe port 1:1.
  53. + *
  54. + * Only MEM access is supported, PAX does not support IO.
  55. + *
  56. + * TODO:
  57. + * MSI interrupts,
  58. + * DRAM > 128 MBytes (e.g. DMA zones)
  59. + */
  60. +
  61. +#include <linux/kernel.h>
  62. +#include <linux/module.h>
  63. +#include <linux/bug.h>
  64. +#include <linux/delay.h>
  65. +#include <linux/pci.h>
  66. +#include <linux/io.h>
  67. +#include <linux/ioport.h>
  68. +#include <linux/interrupt.h>
  69. +#include <linux/bcma/bcma.h>
  70. +
  71. +#define SI_ENUM_BASE 0x18000000 /* Enumeration space base */
  72. +
  73. +/*
  74. + * Register offset definitions
  75. + */
  76. +#define SOC_PCIE_CONTROL 0x000 /* a.k.a. CLK_CONTROL reg */
  77. +#define SOC_PCIE_PM_STATUS 0x008
  78. +#define SOC_PCIE_PM_CONTROL 0x00c /* in EP mode only ! */
  79. +
  80. +#define SOC_PCIE_EXT_CFG_ADDR 0x120
  81. +#define SOC_PCIE_EXT_CFG_DATA 0x124
  82. +#define SOC_PCIE_CFG_ADDR 0x1f8
  83. +#define SOC_PCIE_CFG_DATA 0x1fc
  84. +
  85. +#define SOC_PCIE_SYS_RC_INTX_EN 0x330
  86. +#define SOC_PCIE_SYS_RC_INTX_CSR 0x334
  87. +#define SOC_PCIE_SYS_HOST_INTR_EN 0x344
  88. +#define SOC_PCIE_SYS_HOST_INTR_CSR 0x348
  89. +
  90. +#define SOC_PCIE_HDR_OFF 0x400 /* 256 bytes per function */
  91. +
  92. +/* 32-bit 4KB in-bound mapping windows for Function 0..3, n=0..7 */
  93. +#define SOC_PCIE_SYS_IMAP0(f, n) (0xc00 + ((f) << 9)((n) << 2))
  94. +/* 64-bit in-bound mapping windows for func 0..3 */
  95. +#define SOC_PCIE_SYS_IMAP1(f) (0xc80 + ((f) << 3))
  96. +#define SOC_PCIE_SYS_IMAP2(f) (0xcc0 + ((f) << 3))
  97. +/* 64-bit in-bound address range n=0..2 */
  98. +#define SOC_PCIE_SYS_IARR(n) (0xd00 + ((n) << 3))
  99. +/* 64-bit out-bound address filter n=0..2 */
  100. +#define SOC_PCIE_SYS_OARR(n) (0xd20 + ((n) << 3))
  101. +/* 64-bit out-bound mapping windows n=0..2 */
  102. +#define SOC_PCIE_SYS_OMAP(n) (0xd40 + ((n) << 3))
  103. +
  104. +#define BCM4360_D11AC_ID 0x43a0
  105. +#define BCM4360_D11AC2G_ID 0x43a1
  106. +#define BCM4360_D11AC5G_ID 0x43a2
  107. +#define BCM4352_D11AC_ID 0x43b1 /* 4352 802.11ac dualband device */
  108. +#define BCM4352_D11AC2G_ID 0x43b2 /* 4352 802.11ac 2.4G device */
  109. +#define BCM4352_D11AC5G_ID 0x43b3 /* 4352 802.11ac 5G device */
  110. +
  111. +static int bcma_pcie2_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
  112. +{
  113. + struct pci_sys_data *sys = pdev->sysdata;
  114. + struct bcma_device *bdev = sys->private_data;
  115. +
  116. + return bdev->irq;
  117. +}
  118. +
  119. +static u32 bcma_pcie2_cfg_base(struct bcma_device *bdev, int busno,
  120. + unsigned int devfn, int where)
  121. +{
  122. + int slot = PCI_SLOT(devfn);
  123. + int fn = PCI_FUNC(devfn);
  124. + u32 addr_reg;
  125. +
  126. + if (busno == 0) {
  127. + if (slot >= 1)
  128. + return 0;
  129. + bcma_write32(bdev, SOC_PCIE_EXT_CFG_ADDR, where & 0xffc);
  130. + return SOC_PCIE_EXT_CFG_DATA;
  131. + }
  132. + if (fn > 1)
  133. + return 0;
  134. + addr_reg = (busno & 0xff) << 20 | (slot << 15) | (fn << 12) |
  135. + (where & 0xffc) | (1 & 0x3);
  136. +
  137. + bcma_write32(bdev, SOC_PCIE_CFG_ADDR, addr_reg);
  138. + return SOC_PCIE_CFG_DATA;
  139. +}
  140. +
  141. +static u32 bcma_pcie2_read_config(struct bcma_device *bdev, int busno,
  142. + unsigned int devfn, int where, int size)
  143. +{
  144. + u32 base;
  145. + u32 data_reg;
  146. + u32 mask;
  147. + int shift;
  148. +
  149. + base = bcma_pcie2_cfg_base(bdev, busno, devfn, where);
  150. +
  151. + if (!base)
  152. + return ~0UL;
  153. +
  154. + data_reg = bcma_read32(bdev, base);
  155. +
  156. + /* NS: CLASS field is R/O, and set to wrong 0x200 value */
  157. + if (busno == 0 && devfn == 0) {
  158. + /*
  159. + * RC's class is 0x0280, but Linux PCI driver needs 0x604
  160. + * for a PCIe bridge. So we must fixup the class code
  161. + * to 0x604 here.
  162. + */
  163. + if ((where & 0xffc) == PCI_CLASS_REVISION) {
  164. + data_reg &= 0xff;
  165. + data_reg |= 0x604 << 16;
  166. + }
  167. + }
  168. + /* HEADER_TYPE=00 indicates the port in EP mode */
  169. +
  170. + if (size == 4)
  171. + return data_reg;
  172. +
  173. + mask = (1 << (size * 8)) - 1;
  174. + shift = (where % 4) * 8;
  175. + return (data_reg >> shift) & mask;
  176. +}
  177. +
  178. +static void bcma_pcie2_write_config(struct bcma_device *bdev, int busno,
  179. + unsigned int devfn, int where, int size,
  180. + u32 val)
  181. +{
  182. + u32 base;
  183. + u32 data_reg;
  184. +
  185. + base = bcma_pcie2_cfg_base(bdev, busno, devfn, where);
  186. +
  187. + if (!base)
  188. + return;
  189. +
  190. + if (size < 4) {
  191. + u32 mask = (1 << (size * 8)) - 1;
  192. + int shift = (where % 4) * 8;
  193. +
  194. + data_reg = bcma_read32(bdev, base);
  195. + data_reg &= ~(mask << shift);
  196. + data_reg |= (val & mask) << shift;
  197. + } else {
  198. + data_reg = val;
  199. + }
  200. +
  201. + bcma_write32(bdev, base, data_reg);
  202. +}
  203. +
  204. +static u8 bcma_pcie2_read_config8(struct bcma_device *bdev, int busno,
  205. + unsigned int devfn, int where)
  206. +{
  207. + return bcma_pcie2_read_config(bdev, busno, devfn, where, 1);
  208. +}
  209. +
  210. +static u16 bcma_pcie2_read_config16(struct bcma_device *bdev, int busno,
  211. + unsigned int devfn, int where)
  212. +{
  213. + return bcma_pcie2_read_config(bdev, busno, devfn, where, 2);
  214. +}
  215. +
  216. +static u32 bcma_pcie2_read_config32(struct bcma_device *bdev, int busno,
  217. + unsigned int devfn, int where)
  218. +{
  219. + return bcma_pcie2_read_config(bdev, busno, devfn, where, 4);
  220. +}
  221. +
  222. +static void bcma_pcie2_write_config8(struct bcma_device *bdev, int busno,
  223. + unsigned int devfn, int where, u8 val)
  224. +{
  225. + return bcma_pcie2_write_config(bdev, busno, devfn, where, 1, val);
  226. +}
  227. +
  228. +static void bcma_pcie2_write_config16(struct bcma_device *bdev, int busno,
  229. + unsigned int devfn, int where, u16 val)
  230. +{
  231. + return bcma_pcie2_write_config(bdev, busno, devfn, where, 2, val);
  232. +}
  233. +
  234. +static void bcma_pcie2_write_config32(struct bcma_device *bdev, int busno,
  235. + unsigned int devfn, int where, u32 val)
  236. +{
  237. + return bcma_pcie2_write_config(bdev, busno, devfn, where, 4, val);
  238. +}
  239. +
  240. +static int bcma_pcie2_read_config_pci(struct pci_bus *bus, unsigned int devfn,
  241. + int where, int size, u32 *val)
  242. +{
  243. + struct pci_sys_data *sys = bus->sysdata;
  244. + struct bcma_device *bdev = sys->private_data;
  245. +
  246. + *val = bcma_pcie2_read_config(bdev, bus->number, devfn, where, size);
  247. +
  248. + return PCIBIOS_SUCCESSFUL;
  249. +}
  250. +
  251. +static int bcma_pcie2_write_config_pci(struct pci_bus *bus, unsigned int devfn,
  252. + int where, int size, u32 val)
  253. +{
  254. + struct pci_sys_data *sys = bus->sysdata;
  255. + struct bcma_device *bdev = sys->private_data;
  256. +
  257. + bcma_pcie2_write_config(bdev, bus->number, devfn, where, size, val);
  258. +
  259. + return PCIBIOS_SUCCESSFUL;
  260. +}
  261. +
  262. +/*
  263. + * Check link status, return 0 if link is up in RC mode,
  264. + * otherwise return non-zero
  265. + */
  266. +static int bcma_pcie2_check_link(struct bcma_device *bdev, u32 allow_gen2)
  267. +{
  268. + u32 devfn = 0;
  269. + u8 tmp8;
  270. + u32 tmp32;
  271. +
  272. + tmp32 = bcma_pcie2_read_config32(bdev, 0, devfn, 0xdc);
  273. + tmp32 &= ~0xf;
  274. + if (allow_gen2)
  275. + tmp32 |= 2;
  276. + else {
  277. + /* force PCIE GEN1 */
  278. + tmp32 |= 1;
  279. + }
  280. + bcma_pcie2_write_config32(bdev, 0, devfn, 0xdc, tmp32);
  281. +
  282. + /* See if the port is in EP mode, indicated by header type 00 */
  283. + tmp8 = bcma_pcie2_read_config8(bdev, 0, devfn, PCI_HEADER_TYPE);
  284. + if (tmp8 != PCI_HEADER_TYPE_BRIDGE) {
  285. + dev_info(&bdev->dev, "Port %d in End-Point mode - ignored\n",
  286. + bdev->core_unit);
  287. + return -ENODEV;
  288. + }
  289. +
  290. + return 0;
  291. +}
  292. +
  293. +/*
  294. + * Initializte the PCIe controller
  295. + */
  296. +static void bcma_pcie2_hw_init(struct bcma_device *bdev)
  297. +{
  298. + u32 devfn = 0;
  299. + u32 tmp32;
  300. + u16 tmp16;
  301. +
  302. + /* Change MPS and MRRS to 512 */
  303. + tmp16 = bcma_pcie2_read_config16(bdev, 0, devfn, 0x4d4);
  304. + tmp16 &= ~7;
  305. + tmp16 |= 2;
  306. + bcma_pcie2_write_config16(bdev, 0, devfn, 0x4d4, tmp16);
  307. +
  308. + tmp32 = bcma_pcie2_read_config32(bdev, 0, devfn, 0xb4);
  309. + tmp32 &= ~((7 << 12) | (7 << 5));
  310. + tmp32 |= (2 << 12) | (2 << 5);
  311. + bcma_pcie2_write_config32(bdev, 0, devfn, 0xb4, tmp32);
  312. +
  313. + /* Turn-on Root-Complex (RC) mode, from reset defailt of EP */
  314. +
  315. + /* The mode is set by straps, can be overwritten via DMU
  316. + register <cru_straps_control> bit 5, "1" means RC
  317. + */
  318. +
  319. + /* Send a downstream reset */
  320. + bcma_write32(bdev, SOC_PCIE_CONTROL, 0x3);
  321. + udelay(250);
  322. + bcma_write32(bdev, SOC_PCIE_CONTROL, 0x1);
  323. + mdelay(250);
  324. +
  325. + /* TBD: take care of PM, check we're on */
  326. +}
  327. +
  328. +/*
  329. + * Setup the address translation
  330. + */
  331. +static void bcma_pcie2_map_init(struct bcma_device *bdev)
  332. +{
  333. + unsigned size, i;
  334. + u32 addr;
  335. +
  336. + /*
  337. + * NOTE:
  338. + * All PCI-to-CPU address mapping are 1:1 for simplicity
  339. + */
  340. +
  341. + /* Outbound address translation setup */
  342. + size = SZ_128M;
  343. + addr = bdev->addr_s[0];
  344. + BUG_ON(!addr);
  345. + BUG_ON(addr & ((1 << 25) - 1)); /* 64MB alignment */
  346. +
  347. + for (i = 0; i < 3; i++) {
  348. + const unsigned win_size = SZ_64M;
  349. + /* 64-bit LE regs, write low word, high is 0 at reset */
  350. + bcma_write32(bdev, SOC_PCIE_SYS_OMAP(i), addr);
  351. + bcma_write32(bdev, SOC_PCIE_SYS_OARR(i), addr|0x1);
  352. + addr += win_size;
  353. + if (size >= win_size)
  354. + size -= win_size;
  355. + if (size == 0)
  356. + break;
  357. + }
  358. + WARN_ON(size > 0);
  359. +
  360. + /*
  361. + * Inbound address translation setup
  362. + * Northstar only maps up to 128 MiB inbound, DRAM could be up to 1 GiB.
  363. + *
  364. + * For now allow access to entire DRAM, assuming it is less than 128MiB,
  365. + * otherwise DMA bouncing mechanism may be required.
  366. + * Also consider DMA mask to limit DMA physical address
  367. + */
  368. + size = SZ_128M;
  369. + addr = PHYS_OFFSET;
  370. +
  371. + size >>= 20; /* In MB */
  372. + size &= 0xff; /* Size is an 8-bit field */
  373. +
  374. + WARN_ON(size == 0);
  375. + /* 64-bit LE regs, write low word, high is 0 at reset */
  376. + bcma_write32(bdev, SOC_PCIE_SYS_IMAP1(0), addr | 0x1);
  377. + bcma_write32(bdev, SOC_PCIE_SYS_IARR(1), addr | size);
  378. +
  379. +#ifdef CONFIG_SPARSEMEM
  380. + addr = PHYS_OFFSET2;
  381. + bcma_write32(bdev, SOC_PCIE_SYS_IMAP2(0), addr | 0x1);
  382. + bcma_write32(bdev, SOC_PCIE_SYS_IARR(2), addr | size);
  383. +#endif
  384. +}
  385. +
  386. +/*
  387. + * Setup PCIE Host bridge
  388. + */
  389. +static void bcma_pcie2_bridge_init(struct bcma_device *bdev)
  390. +{
  391. + u32 devfn = 0;
  392. + u8 tmp8;
  393. + u16 tmp16;
  394. +
  395. + bcma_pcie2_write_config8(bdev, 0, devfn, PCI_PRIMARY_BUS, 0);
  396. + bcma_pcie2_write_config8(bdev, 0, devfn, PCI_SECONDARY_BUS, 1);
  397. + bcma_pcie2_write_config8(bdev, 0, devfn, PCI_SUBORDINATE_BUS, 4);
  398. +
  399. + tmp8 = bcma_pcie2_read_config8(bdev, 0, devfn, PCI_PRIMARY_BUS);
  400. + tmp8 = bcma_pcie2_read_config8(bdev, 0, devfn, PCI_SECONDARY_BUS);
  401. + tmp8 = bcma_pcie2_read_config8(bdev, 0, devfn, PCI_SUBORDINATE_BUS);
  402. +
  403. + /* MEM_BASE, MEM_LIM require 1MB alignment */
  404. + BUG_ON((bdev->addr_s[0] >> 16) & 0xf);
  405. + bcma_pcie2_write_config16(bdev, 0, devfn, PCI_MEMORY_BASE,
  406. + bdev->addr_s[0] >> 16);
  407. + BUG_ON(((bdev->addr_s[0] + SZ_128M) >> 16) & 0xf);
  408. + bcma_pcie2_write_config16(bdev, 0, devfn, PCI_MEMORY_LIMIT,
  409. + (bdev->addr_s[0] + SZ_128M) >> 16);
  410. +
  411. + /* These registers are not supported on the NS */
  412. + bcma_pcie2_write_config16(bdev, 0, devfn, PCI_IO_BASE_UPPER16, 0);
  413. + bcma_pcie2_write_config16(bdev, 0, devfn, PCI_IO_LIMIT_UPPER16, 0);
  414. +
  415. + /* Force class to that of a Bridge */
  416. + bcma_pcie2_write_config16(bdev, 0, devfn, PCI_CLASS_DEVICE,
  417. + PCI_CLASS_BRIDGE_PCI);
  418. +
  419. + tmp16 = bcma_pcie2_read_config16(bdev, 0, devfn, PCI_CLASS_DEVICE);
  420. + tmp16 = bcma_pcie2_read_config16(bdev, 0, devfn, PCI_MEMORY_BASE);
  421. + tmp16 = bcma_pcie2_read_config16(bdev, 0, devfn, PCI_MEMORY_LIMIT);
  422. +}
  423. +
  424. +static int bcma_pcie2_allow_gen2_rc(struct bcma_device *bdev)
  425. +{
  426. + u32 vendorid, devid, chipid, chiprev;
  427. + u32 val, bar;
  428. + void __iomem *base;
  429. + int allow = 1;
  430. +
  431. + /* Read PCI vendor/device ID's */
  432. + bcma_write32(bdev, SOC_PCIE_CFG_ADDR, 0x0);
  433. + val = bcma_read32(bdev, SOC_PCIE_CFG_DATA);
  434. + vendorid = val & 0xffff;
  435. + devid = val >> 16;
  436. + if (vendorid == PCI_VENDOR_ID_BROADCOM &&
  437. + (devid == BCMA_CHIP_ID_BCM4360 || devid == BCM4360_D11AC_ID ||
  438. + devid == BCM4360_D11AC2G_ID || devid == BCM4360_D11AC5G_ID ||
  439. + devid == BCM4352_D11AC_ID || devid == BCM4352_D11AC2G_ID ||
  440. + devid == BCM4352_D11AC5G_ID)) {
  441. + /* Config BAR0 */
  442. + bar = bdev->addr_s[0];
  443. + bcma_write32(bdev, SOC_PCIE_CFG_ADDR, 0x10);
  444. + bcma_write32(bdev, SOC_PCIE_CFG_DATA, bar);
  445. + /* Config BAR0 window to access chipc */
  446. + bcma_write32(bdev, SOC_PCIE_CFG_ADDR, 0x80);
  447. + bcma_write32(bdev, SOC_PCIE_CFG_DATA, SI_ENUM_BASE);
  448. +
  449. + /* Enable memory resource */
  450. + bcma_write32(bdev, SOC_PCIE_CFG_ADDR, 0x4);
  451. + val = bcma_read32(bdev, SOC_PCIE_CFG_DATA);
  452. + val |= PCI_COMMAND_MEMORY;
  453. + bcma_write32(bdev, SOC_PCIE_CFG_DATA, val);
  454. + /* Enable memory and bus master */
  455. + bcma_write32(bdev, SOC_PCIE_HDR_OFF + 4, 0x6);
  456. +
  457. + /* Read CHIP ID */
  458. + base = ioremap(bar, 0x1000);
  459. + val = __raw_readl(base);
  460. + iounmap(base);
  461. + chipid = val & 0xffff;
  462. + chiprev = (val >> 16) & 0xf;
  463. + if ((chipid == BCMA_CHIP_ID_BCM4360 ||
  464. + chipid == BCMA_CHIP_ID_BCM43460 ||
  465. + chipid == BCMA_CHIP_ID_BCM4352) && (chiprev < 3))
  466. + allow = 0;
  467. + }
  468. + return allow;
  469. +}
  470. +
  471. +static void bcma_pcie2_3rd_init(struct bcma_bus *bus)
  472. +{
  473. + /* PCIE PLL block register (base 0x8000) */
  474. + bcma_chipco_b_mii_write(&bus->drv_cc_b, 0x00000088, 0x57fe8000);
  475. + /* Check PCIE PLL lock status */
  476. + bcma_chipco_b_mii_write(&bus->drv_cc_b, 0x00000088, 0x67c60000);
  477. +}
  478. +
  479. +/* To improve PCIE phy jitter */
  480. +static void bcma_pcie2_improve_phy_jitter(struct bcma_bus *bus, int phyaddr)
  481. +{
  482. + u32 val;
  483. +
  484. + /* Change blkaddr */
  485. + val = (1 << 30) | (1 << 28) | (phyaddr << 23) | (0x1f << 18) |
  486. + (2 << 16) | (0x863 << 4);
  487. + bcma_chipco_b_mii_write(&bus->drv_cc_b, 0x0000009a, val);
  488. +
  489. + /* Write 0x0190 to 0x13 regaddr */
  490. + val = (1 << 30) | (1 << 28) | (phyaddr << 23) | (0x13 << 18) |
  491. + (2 << 16) | 0x0190;
  492. + bcma_chipco_b_mii_write(&bus->drv_cc_b, 0x0000009a, val);
  493. +
  494. + /* Write 0x0191 to 0x19 regaddr */
  495. + val = (1 << 30) | (1 << 28) | (phyaddr << 23) | (0x19 << 18) |
  496. + (2 << 16) | 0x0191;
  497. + bcma_chipco_b_mii_write(&bus->drv_cc_b, 0x0000009a, val);
  498. +}
  499. +
  500. +static int bcma_pcie2_setup(int nr, struct pci_sys_data *sys)
  501. +{
  502. + struct bcma_device *bdev = sys->private_data;
  503. + struct bcma_bus *bus = bdev->bus;
  504. + struct resource *res;
  505. + struct bcma_device *arm_core;
  506. + u32 cru_straps_ctrl;
  507. + int allow_gen2, linkfail;
  508. + int phyaddr;
  509. +
  510. + if (bdev->core_unit == 2) {
  511. + arm_core = bcma_find_core(bus, BCMA_CORE_ARMCA9);
  512. + cru_straps_ctrl = bcma_read32(arm_core, 0x2a0);
  513. +
  514. + /* 3rd PCIE is not selected */
  515. + if (cru_straps_ctrl & 0x10)
  516. + return -ENODEV;
  517. +
  518. + bcma_pcie2_3rd_init(bus);
  519. + phyaddr = 0xf;
  520. + } else {
  521. + phyaddr = bdev->core_unit;
  522. + }
  523. + bcma_pcie2_improve_phy_jitter(bus, phyaddr);
  524. +
  525. + /* create mem resource */
  526. + res = devm_kzalloc(&bdev->dev, sizeof(*res), GFP_KERNEL);
  527. + if (!res)
  528. + return -EINVAL;
  529. +
  530. + res->start = bdev->addr_s[0];
  531. + res->end = res->start + SZ_128M - 1;
  532. + res->name = "PCIe Configuration Space";
  533. + res->flags = IORESOURCE_MEM;
  534. +
  535. + pci_add_resource(&sys->resources, res);
  536. +
  537. + /* This PCIe controller does not support IO Mem, so use a dummy one. */
  538. + res = devm_kzalloc(&bdev->dev, sizeof(*res), GFP_KERNEL);
  539. + if (!res)
  540. + return -EINVAL;
  541. +
  542. + res->start = bdev->addr_s[0];
  543. + res->end = res->start + SZ_128M - 1;
  544. + res->name = "PCIe Configuration Space";
  545. + res->flags = IORESOURCE_IO;
  546. +
  547. + pci_add_resource(&sys->resources, res);
  548. +
  549. + for (allow_gen2 = 0; allow_gen2 <= 1; allow_gen2++) {
  550. + bcma_pcie2_hw_init(bdev);
  551. + bcma_pcie2_map_init(bdev);
  552. +
  553. + /*
  554. + * Skip inactive ports -
  555. + * will need to change this for hot-plugging
  556. + */
  557. + linkfail = bcma_pcie2_check_link(bdev, allow_gen2);
  558. + if (linkfail)
  559. + break;
  560. +
  561. + bcma_pcie2_bridge_init(bdev);
  562. +
  563. + if (allow_gen2 == 0) {
  564. + if (bcma_pcie2_allow_gen2_rc(bdev) == 0)
  565. + break;
  566. + dev_info(&bdev->dev, "switching to GEN2\n");
  567. + }
  568. + }
  569. +
  570. + if (linkfail)
  571. + return -1;
  572. +
  573. + return 1;
  574. +}
  575. +
  576. +/*
  577. + * Methods for accessing configuration registers
  578. + */
  579. +static struct pci_ops bcma_pcie2_ops = {
  580. + .read = bcma_pcie2_read_config_pci,
  581. + .write = bcma_pcie2_write_config_pci,
  582. +};
  583. +
  584. +static int bcma_pcie2_probe(struct bcma_device *bdev)
  585. +{
  586. + struct hw_pci hw;
  587. +
  588. + dev_info(&bdev->dev, "scanning bus\n");
  589. +
  590. + hw = (struct hw_pci) {
  591. + .nr_controllers = 1,
  592. + .domain = bdev->core_unit,
  593. + .private_data = (void **)&bdev,
  594. + .setup = bcma_pcie2_setup,
  595. + .map_irq = bcma_pcie2_map_irq,
  596. + .ops = &bcma_pcie2_ops,
  597. + };
  598. +
  599. + /* Announce this port to ARM/PCI common code */
  600. + pci_common_init_dev(&bdev->dev, &hw);
  601. +
  602. + /* Setup virtual-wire interrupts */
  603. + bcma_write32(bdev, SOC_PCIE_SYS_RC_INTX_EN, 0xf);
  604. +
  605. + /* Enable memory and bus master */
  606. + bcma_write32(bdev, SOC_PCIE_HDR_OFF + 4, 0x6);
  607. +
  608. + return 0;
  609. +}
  610. +
  611. +static const struct bcma_device_id bcma_pcie2_table[] = {
  612. + BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_NS_PCIEG2, BCMA_ANY_REV, BCMA_ANY_CLASS),
  613. + BCMA_CORETABLE_END
  614. +};
  615. +MODULE_DEVICE_TABLE(bcma, bcma_pcie2_table);
  616. +
  617. +static struct bcma_driver bcma_pcie2_driver = {
  618. + .name = KBUILD_MODNAME,
  619. + .id_table = bcma_pcie2_table,
  620. + .probe = bcma_pcie2_probe,
  621. +};
  622. +
  623. +static int __init bcma_pcie2_init(void)
  624. +{
  625. + return bcma_driver_register(&bcma_pcie2_driver);
  626. +}
  627. +module_init(bcma_pcie2_init);
  628. +
  629. +static void __exit bcma_pcie2_exit(void)
  630. +{
  631. + bcma_driver_unregister(&bcma_pcie2_driver);
  632. +}
  633. +module_exit(bcma_pcie2_exit);
  634. +
  635. +MODULE_AUTHOR("Hauke Mehrtens");
  636. +MODULE_DESCRIPTION("PCIe Gen2 driver for BCMA");
  637. +MODULE_LICENSE("GPLv2");