pci-thunder-pem.c 9.0 KB

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