pcie-rcar.c 30 KB

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