pci-thunder-pem.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. *
  14. * Copyright (C) 2015 - 2016 Cavium, Inc.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_pci.h>
  20. #include <linux/pci-ecam.h>
  21. #include <linux/platform_device.h>
  22. #define PEM_CFG_WR 0x28
  23. #define PEM_CFG_RD 0x30
  24. struct thunder_pem_pci {
  25. u32 ea_entry[3];
  26. void __iomem *pem_reg_base;
  27. };
  28. static int thunder_pem_bridge_read(struct pci_bus *bus, unsigned int devfn,
  29. int where, int size, u32 *val)
  30. {
  31. u64 read_val;
  32. struct pci_config_window *cfg = bus->sysdata;
  33. struct thunder_pem_pci *pem_pci = (struct thunder_pem_pci *)cfg->priv;
  34. if (devfn != 0 || where >= 2048) {
  35. *val = ~0;
  36. return PCIBIOS_DEVICE_NOT_FOUND;
  37. }
  38. /*
  39. * 32-bit accesses only. Write the address to the low order
  40. * bits of PEM_CFG_RD, then trigger the read by reading back.
  41. * The config data lands in the upper 32-bits of PEM_CFG_RD.
  42. */
  43. read_val = where & ~3ull;
  44. writeq(read_val, pem_pci->pem_reg_base + PEM_CFG_RD);
  45. read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
  46. read_val >>= 32;
  47. /*
  48. * The config space contains some garbage, fix it up. Also
  49. * synthesize an EA capability for the BAR used by MSI-X.
  50. */
  51. switch (where & ~3) {
  52. case 0x40:
  53. read_val &= 0xffff00ff;
  54. read_val |= 0x00007000; /* Skip MSI CAP */
  55. break;
  56. case 0x70: /* Express Cap */
  57. /* PME interrupt on vector 2*/
  58. read_val |= (2u << 25);
  59. break;
  60. case 0xb0: /* MSI-X Cap */
  61. /* TableSize=4, Next Cap is EA */
  62. read_val &= 0xc00000ff;
  63. read_val |= 0x0003bc00;
  64. break;
  65. case 0xb4:
  66. /* Table offset=0, BIR=0 */
  67. read_val = 0x00000000;
  68. break;
  69. case 0xb8:
  70. /* BPA offset=0xf0000, BIR=0 */
  71. read_val = 0x000f0000;
  72. break;
  73. case 0xbc:
  74. /* EA, 1 entry, no next Cap */
  75. read_val = 0x00010014;
  76. break;
  77. case 0xc0:
  78. /* DW2 for type-1 */
  79. read_val = 0x00000000;
  80. break;
  81. case 0xc4:
  82. /* Entry BEI=0, PP=0x00, SP=0xff, ES=3 */
  83. read_val = 0x80ff0003;
  84. break;
  85. case 0xc8:
  86. read_val = pem_pci->ea_entry[0];
  87. break;
  88. case 0xcc:
  89. read_val = pem_pci->ea_entry[1];
  90. break;
  91. case 0xd0:
  92. read_val = pem_pci->ea_entry[2];
  93. break;
  94. default:
  95. break;
  96. }
  97. read_val >>= (8 * (where & 3));
  98. switch (size) {
  99. case 1:
  100. read_val &= 0xff;
  101. break;
  102. case 2:
  103. read_val &= 0xffff;
  104. break;
  105. default:
  106. break;
  107. }
  108. *val = read_val;
  109. return PCIBIOS_SUCCESSFUL;
  110. }
  111. static int thunder_pem_config_read(struct pci_bus *bus, unsigned int devfn,
  112. int where, int size, u32 *val)
  113. {
  114. struct pci_config_window *cfg = bus->sysdata;
  115. if (bus->number < cfg->busr.start ||
  116. bus->number > cfg->busr.end)
  117. return PCIBIOS_DEVICE_NOT_FOUND;
  118. /*
  119. * The first device on the bus is the PEM PCIe bridge.
  120. * Special case its config access.
  121. */
  122. if (bus->number == cfg->busr.start)
  123. return thunder_pem_bridge_read(bus, devfn, where, size, val);
  124. return pci_generic_config_read(bus, devfn, where, size, val);
  125. }
  126. /*
  127. * Some of the w1c_bits below also include read-only or non-writable
  128. * reserved bits, this makes the code simpler and is OK as the bits
  129. * are not affected by writing zeros to them.
  130. */
  131. static u32 thunder_pem_bridge_w1c_bits(u64 where_aligned)
  132. {
  133. u32 w1c_bits = 0;
  134. switch (where_aligned) {
  135. case 0x04: /* Command/Status */
  136. case 0x1c: /* Base and I/O Limit/Secondary Status */
  137. w1c_bits = 0xff000000;
  138. break;
  139. case 0x44: /* Power Management Control and Status */
  140. w1c_bits = 0xfffffe00;
  141. break;
  142. case 0x78: /* Device Control/Device Status */
  143. case 0x80: /* Link Control/Link Status */
  144. case 0x88: /* Slot Control/Slot Status */
  145. case 0x90: /* Root Status */
  146. case 0xa0: /* Link Control 2 Registers/Link Status 2 */
  147. w1c_bits = 0xffff0000;
  148. break;
  149. case 0x104: /* Uncorrectable Error Status */
  150. case 0x110: /* Correctable Error Status */
  151. case 0x130: /* Error Status */
  152. case 0x160: /* Link Control 4 */
  153. w1c_bits = 0xffffffff;
  154. break;
  155. default:
  156. break;
  157. }
  158. return w1c_bits;
  159. }
  160. /* Some bits must be written to one so they appear to be read-only. */
  161. static u32 thunder_pem_bridge_w1_bits(u64 where_aligned)
  162. {
  163. u32 w1_bits;
  164. switch (where_aligned) {
  165. case 0x1c: /* I/O Base / I/O Limit, Secondary Status */
  166. /* Force 32-bit I/O addressing. */
  167. w1_bits = 0x0101;
  168. break;
  169. case 0x24: /* Prefetchable Memory Base / Prefetchable Memory Limit */
  170. /* Force 64-bit addressing */
  171. w1_bits = 0x00010001;
  172. break;
  173. default:
  174. w1_bits = 0;
  175. break;
  176. }
  177. return w1_bits;
  178. }
  179. static int thunder_pem_bridge_write(struct pci_bus *bus, unsigned int devfn,
  180. int where, int size, u32 val)
  181. {
  182. struct pci_config_window *cfg = bus->sysdata;
  183. struct thunder_pem_pci *pem_pci = (struct thunder_pem_pci *)cfg->priv;
  184. u64 write_val, read_val;
  185. u64 where_aligned = where & ~3ull;
  186. u32 mask = 0;
  187. if (devfn != 0 || where >= 2048)
  188. return PCIBIOS_DEVICE_NOT_FOUND;
  189. /*
  190. * 32-bit accesses only. If the write is for a size smaller
  191. * than 32-bits, we must first read the 32-bit value and merge
  192. * in the desired bits and then write the whole 32-bits back
  193. * out.
  194. */
  195. switch (size) {
  196. case 1:
  197. writeq(where_aligned, pem_pci->pem_reg_base + PEM_CFG_RD);
  198. read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
  199. read_val >>= 32;
  200. mask = ~(0xff << (8 * (where & 3)));
  201. read_val &= mask;
  202. val = (val & 0xff) << (8 * (where & 3));
  203. val |= (u32)read_val;
  204. break;
  205. case 2:
  206. writeq(where_aligned, pem_pci->pem_reg_base + PEM_CFG_RD);
  207. read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
  208. read_val >>= 32;
  209. mask = ~(0xffff << (8 * (where & 3)));
  210. read_val &= mask;
  211. val = (val & 0xffff) << (8 * (where & 3));
  212. val |= (u32)read_val;
  213. break;
  214. default:
  215. break;
  216. }
  217. /*
  218. * By expanding the write width to 32 bits, we may
  219. * inadvertently hit some W1C bits that were not intended to
  220. * be written. Calculate the mask that must be applied to the
  221. * data to be written to avoid these cases.
  222. */
  223. if (mask) {
  224. u32 w1c_bits = thunder_pem_bridge_w1c_bits(where);
  225. if (w1c_bits) {
  226. mask &= w1c_bits;
  227. val &= ~mask;
  228. }
  229. }
  230. /*
  231. * Some bits must be read-only with value of one. Since the
  232. * access method allows these to be cleared if a zero is
  233. * written, force them to one before writing.
  234. */
  235. val |= thunder_pem_bridge_w1_bits(where_aligned);
  236. /*
  237. * Low order bits are the config address, the high order 32
  238. * bits are the data to be written.
  239. */
  240. write_val = (((u64)val) << 32) | where_aligned;
  241. writeq(write_val, pem_pci->pem_reg_base + PEM_CFG_WR);
  242. return PCIBIOS_SUCCESSFUL;
  243. }
  244. static int thunder_pem_config_write(struct pci_bus *bus, unsigned int devfn,
  245. int where, int size, u32 val)
  246. {
  247. struct pci_config_window *cfg = bus->sysdata;
  248. if (bus->number < cfg->busr.start ||
  249. bus->number > cfg->busr.end)
  250. return PCIBIOS_DEVICE_NOT_FOUND;
  251. /*
  252. * The first device on the bus is the PEM PCIe bridge.
  253. * Special case its config access.
  254. */
  255. if (bus->number == cfg->busr.start)
  256. return thunder_pem_bridge_write(bus, devfn, where, size, val);
  257. return pci_generic_config_write(bus, devfn, where, size, val);
  258. }
  259. static int thunder_pem_init(struct device *dev, struct pci_config_window *cfg,
  260. struct resource *res_pem)
  261. {
  262. struct thunder_pem_pci *pem_pci;
  263. resource_size_t bar4_start;
  264. pem_pci = devm_kzalloc(dev, sizeof(*pem_pci), GFP_KERNEL);
  265. if (!pem_pci)
  266. return -ENOMEM;
  267. pem_pci->pem_reg_base = devm_ioremap(dev, res_pem->start, 0x10000);
  268. if (!pem_pci->pem_reg_base)
  269. return -ENOMEM;
  270. /*
  271. * The MSI-X BAR for the PEM and AER interrupts is located at
  272. * a fixed offset from the PEM register base. Generate a
  273. * fragment of the synthesized Enhanced Allocation capability
  274. * structure here for the BAR.
  275. */
  276. bar4_start = res_pem->start + 0xf00000;
  277. pem_pci->ea_entry[0] = (u32)bar4_start | 2;
  278. pem_pci->ea_entry[1] = (u32)(res_pem->end - bar4_start) & ~3u;
  279. pem_pci->ea_entry[2] = (u32)(bar4_start >> 32);
  280. cfg->priv = pem_pci;
  281. return 0;
  282. }
  283. static int thunder_pem_platform_init(struct pci_config_window *cfg)
  284. {
  285. struct device *dev = cfg->parent;
  286. struct platform_device *pdev = to_platform_device(dev);
  287. struct resource *res_pem;
  288. if (!dev->of_node)
  289. return -EINVAL;
  290. /*
  291. * The second register range is the PEM bridge to the PCIe
  292. * bus. It has a different config access method than those
  293. * devices behind the bridge.
  294. */
  295. res_pem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  296. if (!res_pem) {
  297. dev_err(dev, "missing \"reg[1]\"property\n");
  298. return -EINVAL;
  299. }
  300. return thunder_pem_init(dev, cfg, res_pem);
  301. }
  302. static struct pci_ecam_ops pci_thunder_pem_ops = {
  303. .bus_shift = 24,
  304. .init = thunder_pem_platform_init,
  305. .pci_ops = {
  306. .map_bus = pci_ecam_map_bus,
  307. .read = thunder_pem_config_read,
  308. .write = thunder_pem_config_write,
  309. }
  310. };
  311. static const struct of_device_id thunder_pem_of_match[] = {
  312. { .compatible = "cavium,pci-host-thunder-pem" },
  313. { },
  314. };
  315. static int thunder_pem_probe(struct platform_device *pdev)
  316. {
  317. return pci_host_common_probe(pdev, &pci_thunder_pem_ops);
  318. }
  319. static struct platform_driver thunder_pem_driver = {
  320. .driver = {
  321. .name = KBUILD_MODNAME,
  322. .of_match_table = thunder_pem_of_match,
  323. },
  324. .probe = thunder_pem_probe,
  325. };
  326. builtin_platform_driver(thunder_pem_driver);