pcie-rcar.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /*
  2. * PCIe driver for Renesas R-Car SoCs
  3. * Copyright (C) 2014 Renesas Electronics Europe Ltd
  4. *
  5. * Based on:
  6. * arch/sh/drivers/pci/pcie-sh7786.c
  7. * arch/sh/drivers/pci/ops-sh7786.c
  8. * Copyright (C) 2009 - 2011 Paul Mundt
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/irqdomain.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/msi.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/of_pci.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/pci.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/slab.h>
  30. #define DRV_NAME "rcar-pcie"
  31. #define PCIECAR 0x000010
  32. #define PCIECCTLR 0x000018
  33. #define CONFIG_SEND_ENABLE (1 << 31)
  34. #define TYPE0 (0 << 8)
  35. #define TYPE1 (1 << 8)
  36. #define PCIECDR 0x000020
  37. #define PCIEMSR 0x000028
  38. #define PCIEINTXR 0x000400
  39. #define PCIEMSITXR 0x000840
  40. /* Transfer control */
  41. #define PCIETCTLR 0x02000
  42. #define CFINIT 1
  43. #define PCIETSTR 0x02004
  44. #define DATA_LINK_ACTIVE 1
  45. #define PCIEERRFR 0x02020
  46. #define UNSUPPORTED_REQUEST (1 << 4)
  47. #define PCIEMSIFR 0x02044
  48. #define PCIEMSIALR 0x02048
  49. #define MSIFE 1
  50. #define PCIEMSIAUR 0x0204c
  51. #define PCIEMSIIER 0x02050
  52. /* root port address */
  53. #define PCIEPRAR(x) (0x02080 + ((x) * 0x4))
  54. /* local address reg & mask */
  55. #define PCIELAR(x) (0x02200 + ((x) * 0x20))
  56. #define PCIELAMR(x) (0x02208 + ((x) * 0x20))
  57. #define LAM_PREFETCH (1 << 3)
  58. #define LAM_64BIT (1 << 2)
  59. #define LAR_ENABLE (1 << 1)
  60. /* PCIe address reg & mask */
  61. #define PCIEPALR(x) (0x03400 + ((x) * 0x20))
  62. #define PCIEPAUR(x) (0x03404 + ((x) * 0x20))
  63. #define PCIEPAMR(x) (0x03408 + ((x) * 0x20))
  64. #define PCIEPTCTLR(x) (0x0340c + ((x) * 0x20))
  65. #define PAR_ENABLE (1 << 31)
  66. #define IO_SPACE (1 << 8)
  67. /* Configuration */
  68. #define PCICONF(x) (0x010000 + ((x) * 0x4))
  69. #define PMCAP(x) (0x010040 + ((x) * 0x4))
  70. #define EXPCAP(x) (0x010070 + ((x) * 0x4))
  71. #define VCCAP(x) (0x010100 + ((x) * 0x4))
  72. /* link layer */
  73. #define IDSETR1 0x011004
  74. #define TLCTLR 0x011048
  75. #define MACSR 0x011054
  76. #define MACCTLR 0x011058
  77. #define SCRAMBLE_DISABLE (1 << 27)
  78. /* R-Car H1 PHY */
  79. #define H1_PCIEPHYADRR 0x04000c
  80. #define WRITE_CMD (1 << 16)
  81. #define PHY_ACK (1 << 24)
  82. #define RATE_POS 12
  83. #define LANE_POS 8
  84. #define ADR_POS 0
  85. #define H1_PCIEPHYDOUTR 0x040014
  86. #define H1_PCIEPHYSR 0x040018
  87. /* R-Car Gen2 PHY */
  88. #define GEN2_PCIEPHYADDR 0x780
  89. #define GEN2_PCIEPHYDATA 0x784
  90. #define GEN2_PCIEPHYCTRL 0x78c
  91. #define INT_PCI_MSI_NR 32
  92. #define RCONF(x) (PCICONF(0)+(x))
  93. #define RPMCAP(x) (PMCAP(0)+(x))
  94. #define REXPCAP(x) (EXPCAP(0)+(x))
  95. #define RVCCAP(x) (VCCAP(0)+(x))
  96. #define PCIE_CONF_BUS(b) (((b) & 0xff) << 24)
  97. #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 19)
  98. #define PCIE_CONF_FUNC(f) (((f) & 0x7) << 16)
  99. #define RCAR_PCI_MAX_RESOURCES 4
  100. #define MAX_NR_INBOUND_MAPS 6
  101. struct rcar_msi {
  102. DECLARE_BITMAP(used, INT_PCI_MSI_NR);
  103. struct irq_domain *domain;
  104. struct msi_controller chip;
  105. unsigned long pages;
  106. struct mutex lock;
  107. int irq1;
  108. int irq2;
  109. };
  110. static inline struct rcar_msi *to_rcar_msi(struct msi_controller *chip)
  111. {
  112. return container_of(chip, struct rcar_msi, chip);
  113. }
  114. /* Structure representing the PCIe interface */
  115. struct rcar_pcie {
  116. struct device *dev;
  117. void __iomem *base;
  118. struct list_head resources;
  119. int root_bus_nr;
  120. struct clk *clk;
  121. struct clk *bus_clk;
  122. struct rcar_msi msi;
  123. };
  124. static void rcar_pci_write_reg(struct rcar_pcie *pcie, unsigned long val,
  125. unsigned long reg)
  126. {
  127. writel(val, pcie->base + reg);
  128. }
  129. static unsigned long rcar_pci_read_reg(struct rcar_pcie *pcie,
  130. unsigned long reg)
  131. {
  132. return readl(pcie->base + reg);
  133. }
  134. enum {
  135. RCAR_PCI_ACCESS_READ,
  136. RCAR_PCI_ACCESS_WRITE,
  137. };
  138. static void rcar_rmw32(struct rcar_pcie *pcie, int where, u32 mask, u32 data)
  139. {
  140. int shift = 8 * (where & 3);
  141. u32 val = rcar_pci_read_reg(pcie, where & ~3);
  142. val &= ~(mask << shift);
  143. val |= data << shift;
  144. rcar_pci_write_reg(pcie, val, where & ~3);
  145. }
  146. static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
  147. {
  148. int shift = 8 * (where & 3);
  149. u32 val = rcar_pci_read_reg(pcie, where & ~3);
  150. return val >> shift;
  151. }
  152. /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
  153. static int rcar_pcie_config_access(struct rcar_pcie *pcie,
  154. unsigned char access_type, struct pci_bus *bus,
  155. unsigned int devfn, int where, u32 *data)
  156. {
  157. int dev, func, reg, index;
  158. dev = PCI_SLOT(devfn);
  159. func = PCI_FUNC(devfn);
  160. reg = where & ~3;
  161. index = reg / 4;
  162. /*
  163. * While each channel has its own memory-mapped extended config
  164. * space, it's generally only accessible when in endpoint mode.
  165. * When in root complex mode, the controller is unable to target
  166. * itself with either type 0 or type 1 accesses, and indeed, any
  167. * controller initiated target transfer to its own config space
  168. * result in a completer abort.
  169. *
  170. * Each channel effectively only supports a single device, but as
  171. * the same channel <-> device access works for any PCI_SLOT()
  172. * value, we cheat a bit here and bind the controller's config
  173. * space to devfn 0 in order to enable self-enumeration. In this
  174. * case the regular ECAR/ECDR path is sidelined and the mangled
  175. * config access itself is initiated as an internal bus transaction.
  176. */
  177. if (pci_is_root_bus(bus)) {
  178. if (dev != 0)
  179. return PCIBIOS_DEVICE_NOT_FOUND;
  180. if (access_type == RCAR_PCI_ACCESS_READ) {
  181. *data = rcar_pci_read_reg(pcie, PCICONF(index));
  182. } else {
  183. /* Keep an eye out for changes to the root bus number */
  184. if (pci_is_root_bus(bus) && (reg == PCI_PRIMARY_BUS))
  185. pcie->root_bus_nr = *data & 0xff;
  186. rcar_pci_write_reg(pcie, *data, PCICONF(index));
  187. }
  188. return PCIBIOS_SUCCESSFUL;
  189. }
  190. if (pcie->root_bus_nr < 0)
  191. return PCIBIOS_DEVICE_NOT_FOUND;
  192. /* Clear errors */
  193. rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
  194. /* Set the PIO address */
  195. rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) |
  196. PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR);
  197. /* Enable the configuration access */
  198. if (bus->parent->number == pcie->root_bus_nr)
  199. rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
  200. else
  201. rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
  202. /* Check for errors */
  203. if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
  204. return PCIBIOS_DEVICE_NOT_FOUND;
  205. /* Check for master and target aborts */
  206. if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) &
  207. (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
  208. return PCIBIOS_DEVICE_NOT_FOUND;
  209. if (access_type == RCAR_PCI_ACCESS_READ)
  210. *data = rcar_pci_read_reg(pcie, PCIECDR);
  211. else
  212. rcar_pci_write_reg(pcie, *data, PCIECDR);
  213. /* Disable the configuration access */
  214. rcar_pci_write_reg(pcie, 0, PCIECCTLR);
  215. return PCIBIOS_SUCCESSFUL;
  216. }
  217. static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
  218. int where, int size, u32 *val)
  219. {
  220. struct rcar_pcie *pcie = bus->sysdata;
  221. int ret;
  222. ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_READ,
  223. bus, devfn, where, val);
  224. if (ret != PCIBIOS_SUCCESSFUL) {
  225. *val = 0xffffffff;
  226. return ret;
  227. }
  228. if (size == 1)
  229. *val = (*val >> (8 * (where & 3))) & 0xff;
  230. else if (size == 2)
  231. *val = (*val >> (8 * (where & 2))) & 0xffff;
  232. dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08lx\n",
  233. bus->number, devfn, where, size, (unsigned long)*val);
  234. return ret;
  235. }
  236. /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
  237. static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
  238. int where, int size, u32 val)
  239. {
  240. struct rcar_pcie *pcie = bus->sysdata;
  241. int shift, ret;
  242. u32 data;
  243. ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_READ,
  244. bus, devfn, where, &data);
  245. if (ret != PCIBIOS_SUCCESSFUL)
  246. return ret;
  247. dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08lx\n",
  248. bus->number, devfn, where, size, (unsigned long)val);
  249. if (size == 1) {
  250. shift = 8 * (where & 3);
  251. data &= ~(0xff << shift);
  252. data |= ((val & 0xff) << shift);
  253. } else if (size == 2) {
  254. shift = 8 * (where & 2);
  255. data &= ~(0xffff << shift);
  256. data |= ((val & 0xffff) << shift);
  257. } else
  258. data = val;
  259. ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_WRITE,
  260. bus, devfn, where, &data);
  261. return ret;
  262. }
  263. static struct pci_ops rcar_pcie_ops = {
  264. .read = rcar_pcie_read_conf,
  265. .write = rcar_pcie_write_conf,
  266. };
  267. static void rcar_pcie_setup_window(int win, struct rcar_pcie *pcie,
  268. struct resource *res)
  269. {
  270. /* Setup PCIe address space mappings for each resource */
  271. resource_size_t size;
  272. resource_size_t res_start;
  273. u32 mask;
  274. rcar_pci_write_reg(pcie, 0x00000000, PCIEPTCTLR(win));
  275. /*
  276. * The PAMR mask is calculated in units of 128Bytes, which
  277. * keeps things pretty simple.
  278. */
  279. size = resource_size(res);
  280. mask = (roundup_pow_of_two(size) / SZ_128) - 1;
  281. rcar_pci_write_reg(pcie, mask << 7, PCIEPAMR(win));
  282. if (res->flags & IORESOURCE_IO)
  283. res_start = pci_pio_to_address(res->start);
  284. else
  285. res_start = res->start;
  286. rcar_pci_write_reg(pcie, upper_32_bits(res_start), PCIEPAUR(win));
  287. rcar_pci_write_reg(pcie, lower_32_bits(res_start) & ~0x7F,
  288. PCIEPALR(win));
  289. /* First resource is for IO */
  290. mask = PAR_ENABLE;
  291. if (res->flags & IORESOURCE_IO)
  292. mask |= IO_SPACE;
  293. rcar_pci_write_reg(pcie, mask, PCIEPTCTLR(win));
  294. }
  295. static int rcar_pcie_setup(struct list_head *resource, struct rcar_pcie *pci)
  296. {
  297. struct resource_entry *win;
  298. int i = 0;
  299. /* Setup PCI resources */
  300. resource_list_for_each_entry(win, &pci->resources) {
  301. struct resource *res = win->res;
  302. if (!res->flags)
  303. continue;
  304. switch (resource_type(res)) {
  305. case IORESOURCE_IO:
  306. case IORESOURCE_MEM:
  307. rcar_pcie_setup_window(i, pci, res);
  308. i++;
  309. break;
  310. case IORESOURCE_BUS:
  311. pci->root_bus_nr = res->start;
  312. break;
  313. default:
  314. continue;
  315. }
  316. pci_add_resource(resource, res);
  317. }
  318. return 1;
  319. }
  320. static int rcar_pcie_enable(struct rcar_pcie *pcie)
  321. {
  322. struct pci_bus *bus, *child;
  323. LIST_HEAD(res);
  324. rcar_pcie_setup(&res, pcie);
  325. pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
  326. if (IS_ENABLED(CONFIG_PCI_MSI))
  327. bus = pci_scan_root_bus_msi(pcie->dev, pcie->root_bus_nr,
  328. &rcar_pcie_ops, pcie, &res, &pcie->msi.chip);
  329. else
  330. bus = pci_scan_root_bus(pcie->dev, pcie->root_bus_nr,
  331. &rcar_pcie_ops, pcie, &res);
  332. if (!bus) {
  333. dev_err(pcie->dev, "Scanning rootbus failed");
  334. return -ENODEV;
  335. }
  336. pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
  337. pci_bus_size_bridges(bus);
  338. pci_bus_assign_resources(bus);
  339. list_for_each_entry(child, &bus->children, node)
  340. pcie_bus_configure_settings(child);
  341. pci_bus_add_devices(bus);
  342. return 0;
  343. }
  344. static int phy_wait_for_ack(struct rcar_pcie *pcie)
  345. {
  346. unsigned int timeout = 100;
  347. while (timeout--) {
  348. if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
  349. return 0;
  350. udelay(100);
  351. }
  352. dev_err(pcie->dev, "Access to PCIe phy timed out\n");
  353. return -ETIMEDOUT;
  354. }
  355. static void phy_write_reg(struct rcar_pcie *pcie,
  356. unsigned int rate, unsigned int addr,
  357. unsigned int lane, unsigned int data)
  358. {
  359. unsigned long phyaddr;
  360. phyaddr = WRITE_CMD |
  361. ((rate & 1) << RATE_POS) |
  362. ((lane & 0xf) << LANE_POS) |
  363. ((addr & 0xff) << ADR_POS);
  364. /* Set write data */
  365. rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
  366. rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
  367. /* Ignore errors as they will be dealt with if the data link is down */
  368. phy_wait_for_ack(pcie);
  369. /* Clear command */
  370. rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
  371. rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
  372. /* Ignore errors as they will be dealt with if the data link is down */
  373. phy_wait_for_ack(pcie);
  374. }
  375. static int rcar_pcie_wait_for_dl(struct rcar_pcie *pcie)
  376. {
  377. unsigned int timeout = 10;
  378. while (timeout--) {
  379. if ((rcar_pci_read_reg(pcie, PCIETSTR) & DATA_LINK_ACTIVE))
  380. return 0;
  381. msleep(5);
  382. }
  383. return -ETIMEDOUT;
  384. }
  385. static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
  386. {
  387. int err;
  388. /* Begin initialization */
  389. rcar_pci_write_reg(pcie, 0, PCIETCTLR);
  390. /* Set mode */
  391. rcar_pci_write_reg(pcie, 1, PCIEMSR);
  392. /*
  393. * Initial header for port config space is type 1, set the device
  394. * class to match. Hardware takes care of propagating the IDSETR
  395. * settings, so there is no need to bother with a quirk.
  396. */
  397. rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
  398. /*
  399. * Setup Secondary Bus Number & Subordinate Bus Number, even though
  400. * they aren't used, to avoid bridge being detected as broken.
  401. */
  402. rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
  403. rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
  404. /* Initialize default capabilities. */
  405. rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
  406. rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
  407. PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
  408. rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
  409. PCI_HEADER_TYPE_BRIDGE);
  410. /* Enable data link layer active state reporting */
  411. rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
  412. PCI_EXP_LNKCAP_DLLLARC);
  413. /* Write out the physical slot number = 0 */
  414. rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
  415. /* Set the completion timer timeout to the maximum 50ms. */
  416. rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
  417. /* Terminate list of capabilities (Next Capability Offset=0) */
  418. rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
  419. /* Enable MSI */
  420. if (IS_ENABLED(CONFIG_PCI_MSI))
  421. rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR);
  422. /* Finish initialization - establish a PCI Express link */
  423. rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
  424. /* This will timeout if we don't have a link. */
  425. err = rcar_pcie_wait_for_dl(pcie);
  426. if (err)
  427. return err;
  428. /* Enable INTx interrupts */
  429. rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);
  430. wmb();
  431. return 0;
  432. }
  433. static int rcar_pcie_hw_init_h1(struct rcar_pcie *pcie)
  434. {
  435. unsigned int timeout = 10;
  436. /* Initialize the phy */
  437. phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
  438. phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
  439. phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
  440. phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
  441. phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
  442. phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
  443. phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
  444. phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
  445. phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
  446. phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
  447. phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
  448. phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);
  449. phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
  450. phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
  451. phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);
  452. while (timeout--) {
  453. if (rcar_pci_read_reg(pcie, H1_PCIEPHYSR))
  454. return rcar_pcie_hw_init(pcie);
  455. msleep(5);
  456. }
  457. return -ETIMEDOUT;
  458. }
  459. static int rcar_pcie_hw_init_gen2(struct rcar_pcie *pcie)
  460. {
  461. /*
  462. * These settings come from the R-Car Series, 2nd Generation User's
  463. * Manual, section 50.3.1 (2) Initialization of the physical layer.
  464. */
  465. rcar_pci_write_reg(pcie, 0x000f0030, GEN2_PCIEPHYADDR);
  466. rcar_pci_write_reg(pcie, 0x00381203, GEN2_PCIEPHYDATA);
  467. rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
  468. rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
  469. rcar_pci_write_reg(pcie, 0x000f0054, GEN2_PCIEPHYADDR);
  470. /* The following value is for DC connection, no termination resistor */
  471. rcar_pci_write_reg(pcie, 0x13802007, GEN2_PCIEPHYDATA);
  472. rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
  473. rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
  474. return rcar_pcie_hw_init(pcie);
  475. }
  476. static int rcar_msi_alloc(struct rcar_msi *chip)
  477. {
  478. int msi;
  479. mutex_lock(&chip->lock);
  480. msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
  481. if (msi < INT_PCI_MSI_NR)
  482. set_bit(msi, chip->used);
  483. else
  484. msi = -ENOSPC;
  485. mutex_unlock(&chip->lock);
  486. return msi;
  487. }
  488. static void rcar_msi_free(struct rcar_msi *chip, unsigned long irq)
  489. {
  490. mutex_lock(&chip->lock);
  491. clear_bit(irq, chip->used);
  492. mutex_unlock(&chip->lock);
  493. }
  494. static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
  495. {
  496. struct rcar_pcie *pcie = data;
  497. struct rcar_msi *msi = &pcie->msi;
  498. unsigned long reg;
  499. reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
  500. /* MSI & INTx share an interrupt - we only handle MSI here */
  501. if (!reg)
  502. return IRQ_NONE;
  503. while (reg) {
  504. unsigned int index = find_first_bit(&reg, 32);
  505. unsigned int irq;
  506. /* clear the interrupt */
  507. rcar_pci_write_reg(pcie, 1 << index, PCIEMSIFR);
  508. irq = irq_find_mapping(msi->domain, index);
  509. if (irq) {
  510. if (test_bit(index, msi->used))
  511. generic_handle_irq(irq);
  512. else
  513. dev_info(pcie->dev, "unhandled MSI\n");
  514. } else {
  515. /* Unknown MSI, just clear it */
  516. dev_dbg(pcie->dev, "unexpected MSI\n");
  517. }
  518. /* see if there's any more pending in this vector */
  519. reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
  520. }
  521. return IRQ_HANDLED;
  522. }
  523. static int rcar_msi_setup_irq(struct msi_controller *chip, struct pci_dev *pdev,
  524. struct msi_desc *desc)
  525. {
  526. struct rcar_msi *msi = to_rcar_msi(chip);
  527. struct rcar_pcie *pcie = container_of(chip, struct rcar_pcie, msi.chip);
  528. struct msi_msg msg;
  529. unsigned int irq;
  530. int hwirq;
  531. hwirq = rcar_msi_alloc(msi);
  532. if (hwirq < 0)
  533. return hwirq;
  534. irq = irq_create_mapping(msi->domain, hwirq);
  535. if (!irq) {
  536. rcar_msi_free(msi, hwirq);
  537. return -EINVAL;
  538. }
  539. irq_set_msi_desc(irq, desc);
  540. msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
  541. msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
  542. msg.data = hwirq;
  543. pci_write_msi_msg(irq, &msg);
  544. return 0;
  545. }
  546. static void rcar_msi_teardown_irq(struct msi_controller *chip, unsigned int irq)
  547. {
  548. struct rcar_msi *msi = to_rcar_msi(chip);
  549. struct irq_data *d = irq_get_irq_data(irq);
  550. rcar_msi_free(msi, d->hwirq);
  551. }
  552. static struct irq_chip rcar_msi_irq_chip = {
  553. .name = "R-Car PCIe MSI",
  554. .irq_enable = pci_msi_unmask_irq,
  555. .irq_disable = pci_msi_mask_irq,
  556. .irq_mask = pci_msi_mask_irq,
  557. .irq_unmask = pci_msi_unmask_irq,
  558. };
  559. static int rcar_msi_map(struct irq_domain *domain, unsigned int irq,
  560. irq_hw_number_t hwirq)
  561. {
  562. irq_set_chip_and_handler(irq, &rcar_msi_irq_chip, handle_simple_irq);
  563. irq_set_chip_data(irq, domain->host_data);
  564. return 0;
  565. }
  566. static const struct irq_domain_ops msi_domain_ops = {
  567. .map = rcar_msi_map,
  568. };
  569. static int rcar_pcie_enable_msi(struct rcar_pcie *pcie)
  570. {
  571. struct platform_device *pdev = to_platform_device(pcie->dev);
  572. struct rcar_msi *msi = &pcie->msi;
  573. unsigned long base;
  574. int err;
  575. mutex_init(&msi->lock);
  576. msi->chip.dev = pcie->dev;
  577. msi->chip.setup_irq = rcar_msi_setup_irq;
  578. msi->chip.teardown_irq = rcar_msi_teardown_irq;
  579. msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
  580. &msi_domain_ops, &msi->chip);
  581. if (!msi->domain) {
  582. dev_err(&pdev->dev, "failed to create IRQ domain\n");
  583. return -ENOMEM;
  584. }
  585. /* Two irqs are for MSI, but they are also used for non-MSI irqs */
  586. err = devm_request_irq(&pdev->dev, msi->irq1, rcar_pcie_msi_irq,
  587. IRQF_SHARED | IRQF_NO_THREAD,
  588. rcar_msi_irq_chip.name, pcie);
  589. if (err < 0) {
  590. dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
  591. goto err;
  592. }
  593. err = devm_request_irq(&pdev->dev, msi->irq2, rcar_pcie_msi_irq,
  594. IRQF_SHARED | IRQF_NO_THREAD,
  595. rcar_msi_irq_chip.name, pcie);
  596. if (err < 0) {
  597. dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
  598. goto err;
  599. }
  600. /* setup MSI data target */
  601. msi->pages = __get_free_pages(GFP_KERNEL, 0);
  602. base = virt_to_phys((void *)msi->pages);
  603. rcar_pci_write_reg(pcie, base | MSIFE, PCIEMSIALR);
  604. rcar_pci_write_reg(pcie, 0, PCIEMSIAUR);
  605. /* enable all MSI interrupts */
  606. rcar_pci_write_reg(pcie, 0xffffffff, PCIEMSIIER);
  607. return 0;
  608. err:
  609. irq_domain_remove(msi->domain);
  610. return err;
  611. }
  612. static int rcar_pcie_get_resources(struct platform_device *pdev,
  613. struct rcar_pcie *pcie)
  614. {
  615. struct resource res;
  616. int err, i;
  617. err = of_address_to_resource(pdev->dev.of_node, 0, &res);
  618. if (err)
  619. return err;
  620. pcie->clk = devm_clk_get(&pdev->dev, "pcie");
  621. if (IS_ERR(pcie->clk)) {
  622. dev_err(pcie->dev, "cannot get platform clock\n");
  623. return PTR_ERR(pcie->clk);
  624. }
  625. err = clk_prepare_enable(pcie->clk);
  626. if (err)
  627. goto fail_clk;
  628. pcie->bus_clk = devm_clk_get(&pdev->dev, "pcie_bus");
  629. if (IS_ERR(pcie->bus_clk)) {
  630. dev_err(pcie->dev, "cannot get pcie bus clock\n");
  631. err = PTR_ERR(pcie->bus_clk);
  632. goto fail_clk;
  633. }
  634. err = clk_prepare_enable(pcie->bus_clk);
  635. if (err)
  636. goto err_map_reg;
  637. i = irq_of_parse_and_map(pdev->dev.of_node, 0);
  638. if (!i) {
  639. dev_err(pcie->dev, "cannot get platform resources for msi interrupt\n");
  640. err = -ENOENT;
  641. goto err_map_reg;
  642. }
  643. pcie->msi.irq1 = i;
  644. i = irq_of_parse_and_map(pdev->dev.of_node, 1);
  645. if (!i) {
  646. dev_err(pcie->dev, "cannot get platform resources for msi interrupt\n");
  647. err = -ENOENT;
  648. goto err_map_reg;
  649. }
  650. pcie->msi.irq2 = i;
  651. pcie->base = devm_ioremap_resource(&pdev->dev, &res);
  652. if (IS_ERR(pcie->base)) {
  653. err = PTR_ERR(pcie->base);
  654. goto err_map_reg;
  655. }
  656. return 0;
  657. err_map_reg:
  658. clk_disable_unprepare(pcie->bus_clk);
  659. fail_clk:
  660. clk_disable_unprepare(pcie->clk);
  661. return err;
  662. }
  663. static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
  664. struct of_pci_range *range,
  665. int *index)
  666. {
  667. u64 restype = range->flags;
  668. u64 cpu_addr = range->cpu_addr;
  669. u64 cpu_end = range->cpu_addr + range->size;
  670. u64 pci_addr = range->pci_addr;
  671. u32 flags = LAM_64BIT | LAR_ENABLE;
  672. u64 mask;
  673. u64 size;
  674. int idx = *index;
  675. if (restype & IORESOURCE_PREFETCH)
  676. flags |= LAM_PREFETCH;
  677. /*
  678. * If the size of the range is larger than the alignment of the start
  679. * address, we have to use multiple entries to perform the mapping.
  680. */
  681. if (cpu_addr > 0) {
  682. unsigned long nr_zeros = __ffs64(cpu_addr);
  683. u64 alignment = 1ULL << nr_zeros;
  684. size = min(range->size, alignment);
  685. } else {
  686. size = range->size;
  687. }
  688. /* Hardware supports max 4GiB inbound region */
  689. size = min(size, 1ULL << 32);
  690. mask = roundup_pow_of_two(size) - 1;
  691. mask &= ~0xf;
  692. while (cpu_addr < cpu_end) {
  693. /*
  694. * Set up 64-bit inbound regions as the range parser doesn't
  695. * distinguish between 32 and 64-bit types.
  696. */
  697. rcar_pci_write_reg(pcie, lower_32_bits(pci_addr), PCIEPRAR(idx));
  698. rcar_pci_write_reg(pcie, lower_32_bits(cpu_addr), PCIELAR(idx));
  699. rcar_pci_write_reg(pcie, lower_32_bits(mask) | flags, PCIELAMR(idx));
  700. rcar_pci_write_reg(pcie, upper_32_bits(pci_addr), PCIEPRAR(idx+1));
  701. rcar_pci_write_reg(pcie, upper_32_bits(cpu_addr), PCIELAR(idx+1));
  702. rcar_pci_write_reg(pcie, 0, PCIELAMR(idx + 1));
  703. pci_addr += size;
  704. cpu_addr += size;
  705. idx += 2;
  706. if (idx > MAX_NR_INBOUND_MAPS) {
  707. dev_err(pcie->dev, "Failed to map inbound regions!\n");
  708. return -EINVAL;
  709. }
  710. }
  711. *index = idx;
  712. return 0;
  713. }
  714. static int pci_dma_range_parser_init(struct of_pci_range_parser *parser,
  715. struct device_node *node)
  716. {
  717. const int na = 3, ns = 2;
  718. int rlen;
  719. parser->node = node;
  720. parser->pna = of_n_addr_cells(node);
  721. parser->np = parser->pna + na + ns;
  722. parser->range = of_get_property(node, "dma-ranges", &rlen);
  723. if (!parser->range)
  724. return -ENOENT;
  725. parser->end = parser->range + rlen / sizeof(__be32);
  726. return 0;
  727. }
  728. static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie *pcie,
  729. struct device_node *np)
  730. {
  731. struct of_pci_range range;
  732. struct of_pci_range_parser parser;
  733. int index = 0;
  734. int err;
  735. if (pci_dma_range_parser_init(&parser, np))
  736. return -EINVAL;
  737. /* Get the dma-ranges from DT */
  738. for_each_of_pci_range(&parser, &range) {
  739. u64 end = range.cpu_addr + range.size - 1;
  740. dev_dbg(pcie->dev, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n",
  741. range.flags, range.cpu_addr, end, range.pci_addr);
  742. err = rcar_pcie_inbound_ranges(pcie, &range, &index);
  743. if (err)
  744. return err;
  745. }
  746. return 0;
  747. }
  748. static const struct of_device_id rcar_pcie_of_match[] = {
  749. { .compatible = "renesas,pcie-r8a7779", .data = rcar_pcie_hw_init_h1 },
  750. { .compatible = "renesas,pcie-rcar-gen2", .data = rcar_pcie_hw_init_gen2 },
  751. { .compatible = "renesas,pcie-r8a7790", .data = rcar_pcie_hw_init_gen2 },
  752. { .compatible = "renesas,pcie-r8a7791", .data = rcar_pcie_hw_init_gen2 },
  753. { .compatible = "renesas,pcie-r8a7795", .data = rcar_pcie_hw_init },
  754. {},
  755. };
  756. MODULE_DEVICE_TABLE(of, rcar_pcie_of_match);
  757. static void rcar_pcie_release_of_pci_ranges(struct rcar_pcie *pci)
  758. {
  759. pci_free_resource_list(&pci->resources);
  760. }
  761. static int rcar_pcie_parse_request_of_pci_ranges(struct rcar_pcie *pci)
  762. {
  763. int err;
  764. struct device *dev = pci->dev;
  765. struct device_node *np = dev->of_node;
  766. resource_size_t iobase;
  767. struct resource_entry *win;
  768. err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pci->resources, &iobase);
  769. if (err)
  770. return err;
  771. resource_list_for_each_entry(win, &pci->resources) {
  772. struct resource *parent, *res = win->res;
  773. switch (resource_type(res)) {
  774. case IORESOURCE_IO:
  775. parent = &ioport_resource;
  776. err = pci_remap_iospace(res, iobase);
  777. if (err) {
  778. dev_warn(dev, "error %d: failed to map resource %pR\n",
  779. err, res);
  780. continue;
  781. }
  782. break;
  783. case IORESOURCE_MEM:
  784. parent = &iomem_resource;
  785. break;
  786. case IORESOURCE_BUS:
  787. default:
  788. continue;
  789. }
  790. err = devm_request_resource(dev, parent, res);
  791. if (err)
  792. goto out_release_res;
  793. }
  794. return 0;
  795. out_release_res:
  796. rcar_pcie_release_of_pci_ranges(pci);
  797. return err;
  798. }
  799. static int rcar_pcie_probe(struct platform_device *pdev)
  800. {
  801. struct rcar_pcie *pcie;
  802. unsigned int data;
  803. const struct of_device_id *of_id;
  804. int err;
  805. int (*hw_init_fn)(struct rcar_pcie *);
  806. pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
  807. if (!pcie)
  808. return -ENOMEM;
  809. pcie->dev = &pdev->dev;
  810. platform_set_drvdata(pdev, pcie);
  811. INIT_LIST_HEAD(&pcie->resources);
  812. rcar_pcie_parse_request_of_pci_ranges(pcie);
  813. err = rcar_pcie_get_resources(pdev, pcie);
  814. if (err < 0) {
  815. dev_err(&pdev->dev, "failed to request resources: %d\n", err);
  816. return err;
  817. }
  818. err = rcar_pcie_parse_map_dma_ranges(pcie, pdev->dev.of_node);
  819. if (err)
  820. return err;
  821. of_id = of_match_device(rcar_pcie_of_match, pcie->dev);
  822. if (!of_id || !of_id->data)
  823. return -EINVAL;
  824. hw_init_fn = of_id->data;
  825. pm_runtime_enable(pcie->dev);
  826. err = pm_runtime_get_sync(pcie->dev);
  827. if (err < 0) {
  828. dev_err(pcie->dev, "pm_runtime_get_sync failed\n");
  829. goto err_pm_disable;
  830. }
  831. /* Failure to get a link might just be that no cards are inserted */
  832. err = hw_init_fn(pcie);
  833. if (err) {
  834. dev_info(&pdev->dev, "PCIe link down\n");
  835. err = 0;
  836. goto err_pm_put;
  837. }
  838. data = rcar_pci_read_reg(pcie, MACSR);
  839. dev_info(&pdev->dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
  840. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  841. err = rcar_pcie_enable_msi(pcie);
  842. if (err < 0) {
  843. dev_err(&pdev->dev,
  844. "failed to enable MSI support: %d\n",
  845. err);
  846. goto err_pm_put;
  847. }
  848. }
  849. err = rcar_pcie_enable(pcie);
  850. if (err)
  851. goto err_pm_put;
  852. return 0;
  853. err_pm_put:
  854. pm_runtime_put(pcie->dev);
  855. err_pm_disable:
  856. pm_runtime_disable(pcie->dev);
  857. return err;
  858. }
  859. static struct platform_driver rcar_pcie_driver = {
  860. .driver = {
  861. .name = DRV_NAME,
  862. .of_match_table = rcar_pcie_of_match,
  863. .suppress_bind_attrs = true,
  864. },
  865. .probe = rcar_pcie_probe,
  866. };
  867. module_platform_driver(rcar_pcie_driver);
  868. MODULE_AUTHOR("Phil Edworthy <phil.edworthy@renesas.com>");
  869. MODULE_DESCRIPTION("Renesas R-Car PCIe driver");
  870. MODULE_LICENSE("GPL v2");